IOS 调用照相机、相册功能
来源:互联网 发布:学游泳费用 知乎 编辑:程序博客网 时间:2024/05/16 04:38
首先需要导入<AssetsLibrary/AssetsLibrary.h>、<Photos/Photos.h>这两个框架,然后遵循UIImagePickerControllerDelegate。
调用相册
#pragma mark 选择图片- (void)selectPhoto{ if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusRestricted || [PHPhotoLibrary authorizationStatus] == AVAuthorizationStatusDenied) { NSString *appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleDisplayName"]; if (!appName) appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleName"]; NSString *info = [NSString stringWithFormat:@"请在%@的\"设置-隐私-照片\"选项中,\r允许%@访问你的手机相册。",[UIDevice currentDevice].model,appName]; UIAlertView *showAlert = [[UIAlertView alloc] initWithTitle:nil message:info delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil]; [showAlert show]; }else { //1.首先判断照片源是否可用 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { //2.实例化 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; //2.1设置照片源 imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //2.2是否允许修改 imagePicker.allowsEditing = YES; //2.3设置代理 imagePicker.delegate = self; //2.4显示控制器 [self presentViewController:imagePicker animated:YES completion:nil]; } }}
调用照相机
#pragma mark 拍照- (void)takePhoto{ AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) { NSString *appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleDisplayName"]; if (!appName) appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleName"]; NSString *info = [NSString stringWithFormat:@"请在%@的\"设置-隐私-相机\"选项中,\r允许%@访问你的照相机。",[UIDevice currentDevice].model,appName]; UIAlertView *showAlert = [[UIAlertView alloc] initWithTitle:nil message:info delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil]; [showAlert show]; } else { // 调用相机 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; imagePicker.allowsEditing = YES; imagePicker.delegate = self; [self presentViewController:imagePicker animated:YES completion:nil]; } }}
代理方法
#pragma mark - UIImagePickerController代理#pragma mark 完成- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ UIImage *image = info[@"UIImagePickerControllerEditedImage"]; [picker dismissViewControllerAnimated:YES completion:nil];}#pragma mark 取消- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ [picker dismissViewControllerAnimated:YES completion:nil];}
1 0
- IOS 调用照相机、相册功能
- IOS调用照相机和相册功能
- 调用照相机和相册功能
- 调用照相机和相册功能
- 调用照相机和相册功能
- 调用照相机和相册功能
- 调用照相机和相册功能
- IOS成长之路-调用照相机和相册功能
- IOS成长之路-调用照相机和相册功能
- IOS成长之路-调用照相机和相册功能
- IOS成长之路-调用照相机和相册功能
- iOS如何调用照相机和相册
- 调用系统照相机 相册
- iOS调用系统照相机和闪光灯功能
- IOS 调用系统相册或照相机tab按钮显示中文
- IOS 调用系统相册或照相机tab按钮显示中文
- iOS 调用系统相册,调用系统照相机,调用系统相册,并且对照片进行剪裁
- 调用系统照相机和相册
- 一个基于UIViewController的高度自定义TabBarController
- 四元数 旋转 旋转矩阵 欧拉角互相转换
- Http响应码
- Python multiprocessing需要避免的陷阱
- 深度解读谷歌SyntaxNet:全新TensorFlow自然语言处理模型
- IOS 调用照相机、相册功能
- ntohs, ntohl, htons,htonl的比较和详解
- Codeforces732D Exams
- java反射学习例子
- tomcat
- LeetCode342. Power of Four 简单解法
- LINUX --- 大内核(顺序阅读一)
- yii2自定义报错页面
- 关于SharedSDK中短信注册和分享功能冲突问题的解决办法