這篇文章給大家分享的是有關(guān)ios如何使用AFN上傳相冊(cè)或者拍照?qǐng)D片的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),下花園企業(yè)網(wǎng)站建設(shè),下花園品牌網(wǎng)站建設(shè),網(wǎng)站定制,下花園網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,下花園網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
由于項(xiàng)目中多處需要上傳圖片,我們可以自定義上傳圖片請(qǐng)求,自定義調(diào)取相冊(cè)及拍照,方便多處使用時(shí)調(diào)用。
主要步驟:
1.第一步:請(qǐng)求上傳你選取的相冊(cè)圖片或者拍照?qǐng)D片(經(jīng)過壓縮處理)
2.第二步:獲取到第一步圖片url上傳給服務(wù)器
3.第三步:回顯圖片(當(dāng)然進(jìn)入該界面時(shí)先判斷是否有圖片,無圖則展示占位圖片,否則就回顯圖片)
廢話不多說,直接上代碼:
1)封裝的上傳圖片的網(wǎng)絡(luò)請(qǐng)求(圖片壓縮) QTXUploadImage 文件
// 利用 afn 上傳一張圖片 #import <Foundation/Foundation.h> @interface QTXUploadImage : NSObject // 上傳圖片的網(wǎng)絡(luò)請(qǐng)求(圖片壓縮) + (void)post:(NSString *)url image:(UIImage *)image name:(NSString *)name success:(void (^)(id json))success failure:(void (^)(NSError *error))failure; @end
#import "QTXUploadImage.h" #import "AFNetworking.h" #import "QTXAccount.h" #import "QTXAccountTool.h" @implementation QTXUploadImage /** * 上傳圖片的網(wǎng)絡(luò)請(qǐng)求(圖片壓縮) * * @param url 上傳圖片的網(wǎng)絡(luò)請(qǐng)求地址 * @param name 和后臺(tái)包名一致 * */ + (void)post:(NSString *)url image:(UIImage *)image name:(NSString *)name success:(void (^)(id json))success failure:(void (^)(NSError *error))failure { // 1.創(chuàng)建網(wǎng)絡(luò)管理者 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; // 2.拼接請(qǐng)求參數(shù)url 也可在具體控制器請(qǐng)求里傳入 NSDictionary *dict = @{@"userId" : [QTXAccountTool account].userId}; // 3.發(fā)送請(qǐng)求 [manager POST:url parameters:dict constructingBodyWithBlock: ^void(id<AFMultipartFormData> formData) { NSData *imageData = UIImageJPEGRepresentation(image, 0.5);//進(jìn)行圖片壓縮 // 使用日期生成圖片名稱 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyyMMddHHmmss"; NSString *fileName = [NSString stringWithFormat:@"%@.png",[formatter stringFromDate:[NSDate date]]]; // 任意的二進(jìn)制數(shù)據(jù)MIMEType application/octet-stream [formData appendPartWithFileData:imageData name:name fileName:fileName mimeType:@"image/png"]; } success:^void(NSURLSessionDataTask * task, id responseObject) { if (success) { success(responseObject); } } failure:^void(NSURLSessionDataTask * task, NSError * error) { if (failure) { failure(error); } }]; } @end
2)封裝的拍照/從相冊(cè)選擇 QTXImagePicker 文件
// 拍照/從相冊(cè)選擇 #import <Foundation/Foundation.h> typedef void(^QTXImagePickerFinishAction)(UIImage *image); @interface QTXImagePicker : NSObject /** @param viewController 用于present UIImagePickerController對(duì)象 @param allowsEditing 是否允許用戶編輯圖像 */ + (void)showImagePickerFromViewController:(UIViewController *)viewController allowsEditing:(BOOL)allowsEditing finishAction:(QTXImagePickerFinishAction)finishAction; @end
#import "QTXImagePicker.h" @interface QTXImagePicker()<UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> @property (nonatomic, weak) UIViewController *viewController; @property (nonatomic, copy) QTXImagePickerFinishAction finishAction; @property (nonatomic, assign) BOOL allowsEditing; @end static QTXImagePicker *qtxImagePickerInstance = nil; @implementation QTXImagePicker + (void)showImagePickerFromViewController:(UIViewController *)viewController allowsEditing:(BOOL)allowsEditing finishAction:(QTXImagePickerFinishAction)finishAction { if (qtxImagePickerInstance == nil) { qtxImagePickerInstance = [[QTXImagePicker alloc] init]; } [qtxImagePickerInstance showImagePickerFromViewController:viewController allowsEditing:allowsEditing finishAction:finishAction]; } - (void)showImagePickerFromViewController:(UIViewController *)viewController allowsEditing:(BOOL)allowsEditing finishAction:(QTXImagePickerFinishAction)finishAction { _viewController = viewController; _finishAction = finishAction; _allowsEditing = allowsEditing; UIActionSheet *sheet = nil; if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"從相冊(cè)選擇", nil]; }else { sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"從相冊(cè)選擇", nil]; } UIView *window = [UIApplication sharedApplication].keyWindow; [sheet showInView:window]; } - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex]; if ([title isEqualToString:@"拍照"]) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.sourceType = UIImagePickerControllerSourceTypeCamera; picker.allowsEditing = _allowsEditing; [_viewController presentViewController:picker animated:YES completion:nil]; }else if ([title isEqualToString:@"從相冊(cè)選擇"]) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; // [picker.navigationBar setBarTintColor:QTXNavColor]; // 修改相冊(cè)的導(dǎo)航條的背景顏色 picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [_viewController presentViewController:picker animated:YES completion:nil]; }else { qtxImagePickerInstance = nil; } } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *image = info[UIImagePickerControllerEditedImage]; if (image == nil) { image = info[UIImagePickerControllerOriginalImage]; } if (_finishAction) { _finishAction(image); } [picker dismissViewControllerAnimated:YES completion:^{}]; qtxImagePickerInstance = nil; } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { if (_finishAction) { _finishAction(nil); } [picker dismissViewControllerAnimated:YES completion:^{}]; qtxImagePickerInstance = nil; } @end
當(dāng)使用時(shí),在所需使用的控制器里調(diào)用:
[QTXImagePicker showImagePickerFromViewController:self allowsEditing:YES finishAction:^(UIImage *image) { if (image) { self.idSideImage = image; [QTXUploadImage post:QTX_xsz1Url image:image name:@"xsz1" success:^(id json) { // 第一步請(qǐng)求上傳 QTXLog(@"圖像上傳請(qǐng)求成功 %@", json); self.idSide = json[@"data"]; } failure:^(NSError *error) { QTXLog(@"學(xué)生證圖像上傳請(qǐng)求失敗 %@", error); }]; } }];
第二步和第三步 需要和各位自己后臺(tái)server聯(lián)調(diào),就屬于普通的get/post請(qǐng)求了,這邊就不放代碼了喲
插一下,我們產(chǎn)品沒讓拍照的照片存儲(chǔ)到相冊(cè)
// 存儲(chǔ)圖片名稱:001.png ~ 009.png for (int i = 1; i<=9; i++) { UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"00%d.png", i]]; UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); [NSThread sleepForTimeInterval:1]; }
感謝各位的閱讀!關(guān)于“ios如何使用AFN上傳相冊(cè)或者拍照?qǐng)D片”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
分享標(biāo)題:ios如何使用AFN上傳相冊(cè)或者拍照?qǐng)D片
當(dāng)前鏈接:http://jinyejixie.com/article30/gggdso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、云服務(wù)器、品牌網(wǎng)站制作、App開發(fā)、軟件開發(fā)、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)