iOS 获取系统相机相册

#import <AVFoundation/AVFoundation.h>

#import <AVFoundation/AVCaptureDevice.h>

<UINavigationControllerDelegateUIImagePickerControllerDelegate> 

 

 

 

// sheet样式供用户选择

UIActionSheet *sheet;

if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    sheet = [[UIActionSheet allocinitWithTitle:nil delegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册中选取"nil];

else { // 模拟器无相机

   sheet = [[UIActionSheet allocinitWithTitle: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 = [[UIAlertViewallocinitWithTitle:@"访问相机受限!" message:@""delegate:nil cancelButtonTitle:@"确认"otherButtonTitles:nilnil];

                    [av show];

                    return;

                }

                sourceType = UIImagePickerControllerSourceTypeCamera;

                break;

            }

            case 1: {

                // 相册  用户选择了相册

                NSString *mediaType = AVMediaTypeVideo;

                AVAuthorizationStatus authStatus = [AVCaptureDeviceauthorizationStatusForMediaType:mediaType];

                if(authStatus != AVAuthorizationStatusAuthorized) { // 没有访问权限

                    UIAlertView *av = [[UIAlertViewallocinitWithTitle:@"访问相册受限!" message:@""delegate:nil cancelButtonTitle:@"确认"otherButtonTitles:nilnil];

                    [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 = [[UIAlertViewallocinitWithTitle:@"访问相册受限!" message:@""delegate:nil cancelButtonTitle:@"确认"otherButtonTitles:nilnil];

                [av show];

                return;

            }

            sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        } else {

            return;

        }

    }

    UIImagePickerController *imagePickerController = [[UIImagePickerController allocinit];

    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:^{}];

 

}

posted @ 2015-07-07 15:14  MacroHong  阅读(230)  评论(0)    收藏  举报