Xcode8打开系统相册和摄像头的方法

一、跟以往最主要的区别在于,需要进行info.plist授权操作,具体如下:

1,相册。NSPhotoLibraryUsageDescription

<key>NSPhotoLibraryUsageDescription</key>

    <string> App 需要您的同意才能打开您的相册(此处可以随便写提示的内容)</string>


2,相机。 NSCameraUsageDescription

<key>NSCameraUsageDescription</key>

    <string>App 需要您的同意才能打开您的相机(此处可以随便写提示的内容)</string>

二、下面就是基础的打开相册和摄像头的方法了

1,导入头文件

#import "VPImageCropperViewController.h"

#import <MobileCoreServices/MobileCoreServices.h>

#import <AssetsLibrary/AssetsLibrary.h>

#import "UIImage+Resize.h"

#import "SVProgressHUD.h"

2,实现相应的委托

UIImagePickerControllerDelegate,UIActionSheetDelegate,VPImageCropperDelegate,UINavigationControllerDelegate

3,添加相应的代码

#pragma mark  --设置头像事件

//设置头像事件

-(void)setUserIcon

{

    if ([[[UIDevice currentDevice] systemVersion] floatValue] > 8)

    {

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

        

        UIAlertAction *takePhotoAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){

            

            [self goToImagePicker:YES];

            

            

            //

        }];

        

        [alert addAction:takePhotoAction];

        

        

        UIAlertAction *imageLibAction = [UIAlertAction actionWithTitle:@"从相册选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){

            

            

            

            [self goToImagePicker:NO];

            

        }];

        

        [alert addAction:imageLibAction];

        

        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){

            

            

        }];

        

        [alert addAction:cancelAction];

        

        //        [alert.view setTintColor:UIColorFromRGB(0x50c878)];

        

        [self presentViewController:alert animated:YES completion:nil];

        

    }

    else{

        

        UIActionSheet* _actionSheet = [[UIActionSheet alloc]

                                       initWithTitle:nil

                                       delegate:self

                                       cancelButtonTitle:@"取消"

                                       destructiveButtonTitle:nil

                                       otherButtonTitles:@"拍照", /*@"从相册选择",*/nil];

        

        [_actionSheet showInView:self.view];

        

    }

    

}


#pragma mark - 选相片代理

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

    // 导入图片 or 拍照

    if(buttonIndex == 0)

    {

        [self goToImagePicker:YES];

        

    }

    else if(buttonIndex == 1){

        [self goToImagePicker:NO];

        

    }

}


//yes:照相机    no:相册

-(void)goToImagePicker:(BOOL)camera

{

    if (camera) {

        

        self.isCamera=YES;

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

        {

            //             [PublishVC goToCamera:self delegate:self];

            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

            {

                UIImagePickerController *controller = [[UIImagePickerController alloc] init];

                controller.sourceType = UIImagePickerControllerSourceTypeCamera;

                //[controller setVideoQuality:UIImagePickerControllerQualityTypeLow];

                NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];

                [mediaTypes addObject:(__bridge NSString *)kUTTypeImage];

                controller.mediaTypes = mediaTypes;

                controller.delegate = self;

                [self presentViewController:controller

                                   animated:YES

                                 completion:^(void){

                                 }];

                

                

                

            }

            

        }

        

    }

    else{

        

        self.isCamera=NO;

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])

        {

            UIImagePickerController *controller = [[UIImagePickerController alloc] init];

            controller.sourceTypeUIImagePickerControllerSourceTypePhotoLibrary;

            NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];

            [mediaTypes addObject:(__bridge NSString *)kUTTypeImage];

            controller.mediaTypes = mediaTypes;

            controller.delegate = self;

            [self presentViewController:controller

                               animated:YES

                             completion:^(void){

                             }];

            

            

        }

        

    }

}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    [picker dismissViewControllerAnimated:YES completion:^() {

        

        UIImage *editedImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

        

        if (self.isCamera) {

            if (editedImage.size.width > SIZE_AVATAR) {

                self.imageToSave = [editedImage resizedImage:CGSizeMake(SIZE_AVATAR, SIZE_AVATAR) interpolationQuality:kCGInterpolationDefault];

            }

            else

            {

                self.imageToSave = editedImage;

            }

            

            NSLog(@"1");

            //提示框

            [SVProgressHUD  showSuccessWithStatus:@"正在上传图片"];

            //调用设置头像接口和刷新UI

            [self.indInfoTableView reloadData];

           }

        else{

            int wantedPhotoWidth = kScreenWidth;

            int clipHight = (kScreenHeight - wantedPhotoWidth) /2;

            VPImageCropperViewController *imgEditorVC = [[VPImageCropperViewController alloc] initWithImage:editedImage cropFrame:CGRectMake(0, clipHight, wantedPhotoWidth, wantedPhotoWidth) limitScaleRatio:3.0];

            imgEditorVC.delegate = self;

            [self presentViewController:imgEditorVC animated:NO completion:nil];

            NSLog(@"2");

        }

        

    }];

}



#pragma mark VPImageCropperDelegate

- (void)imageCropper:(VPImageCropperViewController *)cropperViewController didFinished:(UIImage *)editedImage {

    

    self.imageToSave = nil;

   NSLog(@"3");

    //缩放图片

    if (editedImage.size.width > SIZE_AVATAR) {

        self.imageToSave = [editedImage resizedImage:CGSizeMake(SIZE_AVATAR, SIZE_AVATAR) interpolationQuality:kCGInterpolationDefault];

    }

    else

    {

        self.imageToSave = editedImage;

    }

    

    

    

    

    //提示框

    [SVProgressHUD  showSuccessWithStatus:@"正在上传图片"];

    

    

    //调用设置头像接口和刷新UI

    [cropperViewController dismissViewControllerAnimated:YES completion:^{

    }];

    

    [self.indInfoTableView reloadData];

}

- (void)imageCropperDidCancel:(VPImageCropperViewController *)cropperViewController

{

    NSLog(@"4");

    [cropperViewController dismissViewControllerAnimated:YES completion:^{

    }];

    

}

此处的 self.imageToSave和self.isCamera分别是UIImage和Bool的两个属性值。

好了,至此就完成了基本的操作。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值