ios最新调用手机相册选取头像(UIActionSheet过期)

Posted 孙富有(iOS工程师)

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios最新调用手机相册选取头像(UIActionSheet过期)相关的知识,希望对你有一定的参考价值。

由于

UIActionSheet过期所以可以使用如下调用手机相册

前提不要忘记添加代理如下两个

UIImagePickerControllerDelegate,UINavigationControllerDelegate

还需要去plist文件里面添加相机相册权限否则要崩溃的哟

//更换头像

- (IBAction)changeHeadIM:(id)sender {

    //创建UIImagePickerController对象,并设置代理和可编辑

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

    imagePicker.editing = YES;

    imagePicker.delegate = self;

    imagePicker.allowsEditing = YES;

    

    //创建sheet提示框,提示选择相机还是相册

    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"请选择打开方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    

    //相机选项

    UIAlertAction * camera = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        //选择相机时,设置UIImagePickerController对象相关属性

        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

        imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;

        imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;

        //跳转到UIImagePickerController控制器弹出相机

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

    }];

    

    //相册选项

    UIAlertAction * photo = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        //选择相册时,设置UIImagePickerController对象相关属性

        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        //跳转到UIImagePickerController控制器弹出相册

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

    }];

    

    //取消按钮

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

        [self dismissViewControllerAnimated:YES completion:nil];

    }];

    

    //添加各个按钮事件

    [alert addAction:camera];

    [alert addAction:photo];

    [alert addAction:cancel];

    

    //弹出sheet提示框

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

}

 

#pragma mark - image picker delegte

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

{

    [picker dismissViewControllerAnimated:YES completion:^{}];

    

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    //    //原图用image.size.width  /  image.size.height

    //    //压缩

    //    UIGraphicsBeginImageContext(CGSizeMake(800, 600));  //size 为CGSize类型,即你所需要的图片尺寸

    //    [image drawInRect:CGRectMake(0, 0, 800, 600)];

    //    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

    //    UIGraphicsEndImageContext();

    

    float  scales = image.size.height / image.size.width;

    UIImage *normalImg;

    //如果需要改动被压大小,调整scale,而不是kk或aa

    if (image.size.width > 600 || image.size.height > 800) {//这里的1000就是scale,所有的都要随着改变

        if (scales > 1) {

            CGSize newSize = CGSizeMake(600 / scales, 800);

            UIGraphicsBeginImageContext(newSize);

            [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];

            normalImg = UIGraphicsGetImageFromCurrentImageContext();

        }else {

            CGSize newSize = CGSizeMake(600 ,800 * scales);

            UIGraphicsBeginImageContext(newSize);

            [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

            normalImg = UIGraphicsGetImageFromCurrentImageContext();

        }

    }else {

        normalImg=image;

    }

    NSData *data = UIImagePNGRepresentation(normalImg);

    self.editHeadIM.image = normalImg;

}

 

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    [self dismissViewControllerAnimated:YES completion:^{}];

}

 

以上是关于ios最新调用手机相册选取头像(UIActionSheet过期)的主要内容,如果未能解决你的问题,请参考以下文章

iOS开发之调用手机摄像头和相册

iOS之保存图片到系统相册和从系统相册选取一张或者多张照片

怎么用html5或js调用手机的摄像头拍照上传以及调用手机相册选取照片

怎么用html5或js调用手机的摄像头拍照上传以及调用手机相册选取照片

如何调用android的拍照或本地相册选取,然后再实现相片上传服务器

微信小程序开发选取相册照片/拍照API调用