您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關ios如何使用AFN上傳相冊或者拍照圖片的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
由于項目中多處需要上傳圖片,我們可以自定義上傳圖片請求,自定義調取相冊及拍照,方便多處使用時調用。
主要步驟:
1.第一步:請求上傳你選取的相冊圖片或者拍照圖片(經過壓縮處理)
2.第二步:獲取到第一步圖片url上傳給服務器
3.第三步:回顯圖片(當然進入該界面時先判斷是否有圖片,無圖則展示占位圖片,否則就回顯圖片)
廢話不多說,直接上代碼:
1)封裝的上傳圖片的網絡請求(圖片壓縮) QTXUploadImage 文件
// 利用 afn 上傳一張圖片 #import <Foundation/Foundation.h> @interface QTXUploadImage : NSObject // 上傳圖片的網絡請求(圖片壓縮) + (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 /** * 上傳圖片的網絡請求(圖片壓縮) * * @param url 上傳圖片的網絡請求地址 * @param name 和后臺包名一致 * */ + (void)post:(NSString *)url image:(UIImage *)image name:(NSString *)name success:(void (^)(id json))success failure:(void (^)(NSError *error))failure { // 1.創建網絡管理者 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; // 2.拼接請求參數url 也可在具體控制器請求里傳入 NSDictionary *dict = @{@"userId" : [QTXAccountTool account].userId}; // 3.發送請求 [manager POST:url parameters:dict constructingBodyWithBlock: ^void(id<AFMultipartFormData> formData) { NSData *imageData = UIImageJPEGRepresentation(image, 0.5);//進行圖片壓縮 // 使用日期生成圖片名稱 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyyMMddHHmmss"; NSString *fileName = [NSString stringWithFormat:@"%@.png",[formatter stringFromDate:[NSDate date]]]; // 任意的二進制數據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)封裝的拍照/從相冊選擇 QTXImagePicker 文件
// 拍照/從相冊選擇 #import <Foundation/Foundation.h> typedef void(^QTXImagePickerFinishAction)(UIImage *image); @interface QTXImagePicker : NSObject /** @param viewController 用于present UIImagePickerController對象 @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:@"拍照", @"從相冊選擇", nil]; }else { sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"從相冊選擇", 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:@"從相冊選擇"]) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; // [picker.navigationBar setBarTintColor:QTXNavColor]; // 修改相冊的導航條的背景顏色 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
當使用時,在所需使用的控制器里調用:
[QTXImagePicker showImagePickerFromViewController:self allowsEditing:YES finishAction:^(UIImage *image) { if (image) { self.idSideImage = image; [QTXUploadImage post:QTX_xsz1Url image:image name:@"xsz1" success:^(id json) { // 第一步請求上傳 QTXLog(@"圖像上傳請求成功 %@", json); self.idSide = json[@"data"]; } failure:^(NSError *error) { QTXLog(@"學生證圖像上傳請求失敗 %@", error); }]; } }];
第二步和第三步 需要和各位自己后臺server聯調,就屬于普通的get/post請求了,這邊就不放代碼了喲
插一下,我們產品沒讓拍照的照片存儲到相冊
// 存儲圖片名稱: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]; }
感謝各位的閱讀!關于“ios如何使用AFN上傳相冊或者拍照圖片”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。