iOS 获取系统相机相册
#import <AVFoundation/AVFoundation.h>
#import <AVFoundation/AVCaptureDevice.h>
<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
// 以sheet样式供用户选择
UIActionSheet *sheet;
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册中选取", nil];
} else { // 模拟器无相机
sheet = [[UIActionSheet alloc] initWithTitle:nildelegate:self cancelButtonTitle:@"取消"destructiveButtonTitle:nil otherButtonTitles:@"从相册中选取", nil];
}
[sheet showInView:self.view];
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
UIImagePickerControllerSourceType sourceType;
// 判断是否支持相机
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
switch (buttonIndex) {
case 0: {
// 相机 用户选择了相机
NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDeviceauthorizationStatusForMediaType:mediaType];
if(authStatus != AVAuthorizationStatusAuthorized) { // 没有访问权限
UIAlertView *av = [[UIAlertViewalloc] initWithTitle:@"访问相机受限!" message:@""delegate:nil cancelButtonTitle:@"确认"otherButtonTitles:nil, nil];
[av show];
return;
}
sourceType = UIImagePickerControllerSourceTypeCamera;
break;
}
case 1: {
// 相册 用户选择了相册
NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDeviceauthorizationStatusForMediaType:mediaType];
if(authStatus != AVAuthorizationStatusAuthorized) { // 没有访问权限
UIAlertView *av = [[UIAlertViewalloc] initWithTitle:@"访问相册受限!" message:@""delegate:nil cancelButtonTitle:@"确认"otherButtonTitles:nil, nil];
[av show];
return;
}
sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
break;
}
case 2: {
// 取消
return;
}
}
} else {
if (buttonIndex == 0) {
// 相册 用户选择了相册
NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDeviceauthorizationStatusForMediaType:mediaType];
if(authStatus != AVAuthorizationStatusAuthorized) { // 没有访问权限
UIAlertView *av = [[UIAlertViewalloc] initWithTitle:@"访问相册受限!" message:@""delegate:nil cancelButtonTitle:@"确认"otherButtonTitles:nil, nil];
[av show];
return;
}
sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
} else {
return;
}
}
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = sourceType;
imagePickerController.allowsEditing = YES;
imagePickerController.delegate = self;
[selfpresentViewController:imagePickerController animated:YES completion:^{}];
}
#pragma mark - UIImagePickerControllerDelegate UINavigationControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YEScompletion:^{}];
// 获取到的图片
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
_headIV.image = image;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YEScompletion:^{}];
}

浙公网安备 33010602011771号