[一句秒懂]iOS调用相机和相册-细节化
2016-06-23 00:00
393 查看
摘要: 详细介绍一下调用相机和相册的细节
1:首先遵守这两个代理:
UIImagePickerControllerDelegate
UINavigationControllerDelegate
2:这是实现UIImagePickerControllerDelegate代理获得到的图片(相机选择和拍照的图片)
3:上传图片:这个上传服务器的方法可能不一样,但是内容大致一样
4:选择照片之前权限的判断
5:选择拍照照片之前的权限的判断
6:弹窗提示封装在一个方法里
友情提示:
在我们链接手机真机测试或者模拟器测试的时候,如果我们没有权限访问的时候,当我们去手机或者模拟器设置-隐私-相机或者相册修改权限的时候,这时候系统会将程序重新启动,从而先杀死,引起表面的崩溃,但是这不是问题,当我们在手机上测试,没有连接电脑程序的时候,就不会出现这样的问题,对于刚开始摸不到头绪的人来说可能吓了一跳,哈哈,不要担心了哈
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<NSString *,id> *)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]; }
友情提示:
在我们链接手机真机测试或者模拟器测试的时候,如果我们没有权限访问的时候,当我们去手机或者模拟器设置-隐私-相机或者相册修改权限的时候,这时候系统会将程序重新启动,从而先杀死,引起表面的崩溃,但是这不是问题,当我们在手机上测试,没有连接电脑程序的时候,就不会出现这样的问题,对于刚开始摸不到头绪的人来说可能吓了一跳,哈哈,不要担心了哈
相关文章推荐
- [一句秒懂]iOS 正则匹配
- iOS 高效添加圆角效果实战讲解
- iOS Quartz2D下载进度条代码分析
- iOS 开发必备的Xcode插件(快捷键)
- iOS动画进阶 - CAKeyframeAnimation实现过山车动画
- iOS block的用法
- iOS动画进阶 - CAKeyframeAnimation实现过山车动画
- iOS开发多线程篇—线程的状态
- iOS7之后设置NavigationBar的背景
- DM365 spi_norflash无操作系统启动和sys_bios启动(一)
- ios企业版发布
- iOS 基础篇14- KVO的使用
- iBeacon在iOS开发中的使用
- ios添加第三方字体
- iOS过滤数据字典
- IOS 数据存储之 Core Data详解
- IOS 自定义从底部弹上来的View
- ios流媒体直播整个框架介绍(HLS、RTSP)
- IOS could not find developer disk image 真机设备不支持
- iOS开发——WAVE音频文件解析