ios-获取相册相机图片
ios-获取相册相机图片
前篇
- 隐私信息 info.plist 配置: https://stackoverflow.com/questions/39465687/nscamerausagedescription-in-ios-10-0-runtime-crash
- iOS关于相机相册权限设置 - https://www.jianshu.com/p/84df2ca84ade
- iOS 获取图片(拍照、相册、图库)详解 - https://www.jianshu.com/p/39a6a104d3c1
- iOS 拍照(相机)/相册获取图片 - https://www.jianshu.com/p/5465e87d8a0a
- iOS关于相机相册权限设置 - https://www.jianshu.com/p/84df2ca84ade
- iOS开发:调用系统自带相机以及获取相册照片的功能实现 - https://www.cnblogs.com/sundaysgarden/articles/9188975.html
获取相册图片
在 info.plist 中配置相册权限提示:
1
2Key : Privacy - Photo Library Usage Description
Value : $(PRODUCT_NAME) photo use实现 <UIKit/UIKit.h> 中 UIImagePickerControllerDelegate 代理接口
1
2
3
4// 当用户选取完成后调用;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { ... }
// 当用户取消选取时调用;
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { ... }获取相册
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#import <Photos/Photos.h>
// 检查相册是否可用
+(BOOL)checkGallery{
PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];
if (authStatus == PHAuthorizationStatusRestricted || authStatus == PHAuthorizationStatusDenied) {
return NO;
}
return YES;
}
// 获取相册
-(void)getGallery:(CPicture*)data cb:(CodeMsgFn)cb {
self.data = data;
self.cb = cb;
//本地相册不需要检查,因为UIImagePickerController会自动检查并提醒
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
picker.allowsEditing = data.isEdit; // 设置选择后的图片可被编辑
BOOL isOk = [PicHelper checkGallery]; // 先检查相机可用是否
if (isOk == NO) {
[self doCallback:ECodePermissionDenyError msg:@"--- no gallery permission"];
} else {
[Tool presentVC:picker]; // 打开相册
}
}
相机拍照
在 info.plist 中配置相机权限提示:
1
2Key : Privacy - Camera Usage Description
Value : $(PRODUCT_NAME) camera use实现 <UIKit/UIKit.h> 中 UIImagePickerControllerDelegate 代理接口 (和相册一样)
1
2
3
4// 当用户选取完成后调用;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { ... }
// 当用户取消选取时调用;
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { ... }打开相机
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
// 检查相机是否可用
+(BOOL)checkCamera{ // 如果用户点击 不允许, 则为 0
NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
if(AVAuthorizationStatusRestricted == authStatus || AVAuthorizationStatusDenied == authStatus) {
return NO;
}
return YES;
}
// 相机拍照
-(void)takePicture:(CPicture*)data cb:(CodeMsgFn)cb {
self.data = data;
self.cb = cb;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
picker.allowsEditing = data.isEdit; // 设置拍照后的图片可被编辑
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
BOOL isOk = [PicHelper checkCamera]; // 先检查相机可用是否
if (isOk == NO) {
[self doCallback:ECodePermissionDenyError msg:@"--- no camera permission"];
} else {
[Tool presentVC:picker]; // 打开相机
}
} else {
[self doCallback:ECodeUnknown msg:@"--- not isSourceTypeAvailable, you device may be iphonesimulator"];
}
}测试
踩坑
调用拍照闪退
报错信息: he app's Info.plist must contain an NSCameraUsageDescription key with a string value explaining to the user how the app uses this data
就是需要在 info.plist 中配置相机提示.