十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章给大家分享的是有关ios如何使用AFN上传相册或者拍照图片的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
创新互联建站是一家集网站建设,下花园企业网站建设,下花园品牌网站建设,网站定制,下花园网站建设报价,网络营销,网络优化,下花园网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
由于项目中多处需要上传图片,我们可以自定义上传图片请求,自定义调取相册及拍照,方便多处使用时调用。
主要步骤:
1.第一步:请求上传你选取的相册图片或者拍照图片(经过压缩处理)
2.第二步:获取到第一步图片url上传给服务器
3.第三步:回显图片(当然进入该界面时先判断是否有图片,无图则展示占位图片,否则就回显图片)
废话不多说,直接上代码:
1)封装的上传图片的网络请求(图片压缩) QTXUploadImage 文件
// 利用 afn 上传一张图片 #import@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(idformData) { 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 文件
// 拍照/从相册选择 #importtypedef 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()@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上传相册或者拍照图片”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!