博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[一句秒懂]iOS调用相机和相册-细节化
阅读量:6855 次
发布时间:2019-06-26

本文共 5376 字,大约阅读时间需要 17 分钟。

  hot3.png

1:首先遵守这两个代理:

UIImagePickerControllerDelegate

UINavigationControllerDelegate

#pragma mark - 调用相机 && 拍照- (void)chooseImagePicker {        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"选择照片来源" preferredStyle:UIAlertControllerStyleActionSheet];        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];    self.imagePickerController = imagePickerController;    imagePickerController.delegate = self;    imagePickerController.allowsEditing = YES;        UIAlertAction *albumAction = [UIAlertAction actionWithTitle:@"从相册选取" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {        [self chooseAlbum];    }];        UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"拍照" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {        [self takePhoto];    }];        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction *action){}];        // 弹出UIAlertAction窗口    [self presentViewController:alertController animated:YES completion:nil];        //用来判断来源 Xcode中的模拟器是没有拍摄功能的,当用模拟器的时候我们不需要把拍照功能加速    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {                [alertController addAction:albumAction];        [alertController addAction:photoAction];        [alertController addAction:cancelAction];    }else {                [alertController addAction:albumAction];        [alertController addAction:cancelAction];    }}

 

2:这是实现UIImagePickerControllerDelegate代理获得到的图片(相机选择和拍照的图片)

#pragma mark - UIImagePickerControllerDelegate代理- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary
*)info { [picker dismissViewControllerAnimated:YES completion:^{}]; //选取裁剪后的图片 UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; // 上传图片 [self upLoadImage:image];}#pragma mark - 取消选择照片-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { YLLog(@"取消"); [picker dismissViewControllerAnimated:YES completion:^{}];}

 

3:上传图片:这个上传服务器的方法可能不一样,但是内容大致一样

#pragma mark - 上传图像- (void)upLoadImage:(UIImage *)image {    [self hudShowLoad];    YLUploadParam *uploadParam = [YLUploadParam new];    uploadParam.data  = UIImageJPEGRepresentation(image, 1);    uploadParam.name = @"file";    uploadParam.fileName = @"avatar.png";    uploadParam.mimeType = @"image/png";        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];    parameters[@"method"] = @"user.setavatar";    parameters[@"userid"] = self.uid;        [YLHttpTool Upload:kApisetAvatar parameters:parameters uploadParam:uploadParam progress:^(id progress) {    } success:^(id responseObject) {        [self hudHide];        if ([responseObject[@"status"] integerValue] == kHttpOk) {            [self hudShowSuccess:@"上传成功"];            YLLog(@"----%@",responseObject[@"data"]);            self.model.avatar = responseObject[@"data"];            NSIndexPath*index=[NSIndexPath indexPathForRow:0 inSection:0];            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:index,nil] withRowAnimation:UITableViewRowAnimationNone];        }else {            [self hudCustom:responseObject[@"msg"] withIcon:nil];        }    } failure:^(NSError *error) {        [self hudHide];    } getFailure:^(NSString *error) {        [self hudHide];    }];}

 

4:选择照片之前权限的判断

/** *  选择照片 */- (void)chooseAlbum {        PHAuthorizationStatus albumStatus = [PHPhotoLibrary authorizationStatus];    if (albumStatus == PHAuthorizationStatusDenied || albumStatus == PHAuthorizationStatusRestricted) { //用户限制和拒绝了(无权限)        //[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];        NSString *message = @"请求访问您的相册,请到设置->隐私->相册 -> 进行相应的授权";        [self addAlertController:message];    }else { // 有权限        _imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;        [self presentViewController:_imagePickerController animated:YES completion:^{}];    }    }

 

5:选择拍照照片之前的权限的判断

/** *  拍照 */- (void)takePhoto {    AVAuthorizationStatus videoStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];    if (videoStatus == AVAuthorizationStatusRestricted || videoStatus == AVAuthorizationStatusDenied) { // 未授权        NSString *message = @"请求访问您的相机,请到设置->隐私->相机 -> 进行相应的授权";        [self addAlertController:message];    }else {  //授权了        _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;        [self presentViewController:_imagePickerController animated:YES completion:^{        }];    }}

6:弹窗提示封装在一个方法里

/** *  权限提示框 */- (void)addAlertController:(NSString *)message {    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];    [alertC addAction:[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {}]];    [self presentViewController:alertC animated:YES completion:nil];}

 

友情提示:

在我们链接手机真机测试或者模拟器测试的时候,如果我们没有权限访问的时候,当我们去手机或者模拟器设置-隐私-相机或者相册修改权限的时候,这时候系统会将程序重新启动,从而先杀死,引起表面的崩溃,但是这不是问题,当我们在手机上测试,没有连接电脑程序的时候,就不会出现这样的问题,对于刚开始摸不到头绪的人来说可能吓了一跳,哈哈,不要担心了哈

 

转载于:https://my.oschina.net/shengbingli/blog/699884

你可能感兴趣的文章
day14-css边框以及其他常用样式
查看>>
还原数据库 提示数据在访问中,无法独占访问
查看>>
【其他】Xshell秘钥方式登陆服务器
查看>>
Codeforces 405C
查看>>
Tomcat之catalina.out日志分割
查看>>
Ubuntu下安装Matplotlib和basemap
查看>>
Fedora17 64bit 安装ORACLE 11gR2
查看>>
[转]以安装桌面体验功能为例来探索windows2012服务器管理器的新变化
查看>>
php 发送POST 请求
查看>>
angularjs四大核心特性
查看>>
售前工程师的成长---一个老员工的经验之谈(四)(转载)
查看>>
201571030329/201571030310《小学四则运算练习软件需求获取》结对项目报告
查看>>
JS数组攻略
查看>>
【高德地图Android SDK】视频教学
查看>>
scanf&scanf_s difference
查看>>
Junit中Assert.assertEquals()和Assert.assertSame方法有什么异同
查看>>
iOS应用App Store发布流程
查看>>
Angular学习笔记--last_update 20151106
查看>>
Redis 为什么使用单进程单线程方式也这么快(转载)
查看>>
iOS开发微信支付的介绍与实现
查看>>