zoukankan      html  css  js  c++  java
  • 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:^{}];

    }

  • 相关阅读:
    【转】sublime text 2 中文乱码解决办法
    vi使用入门指南
    【原创】基于部署映像服务和管理(DISM)修改映象解决WIN7 USB3.0安装时报错
    日语五十音图快速记忆法
    深度阅读:C语言指针,从底层原理到花式技巧,图文+代码透析
    全民热衷“合成大西瓜”,游戏外挂上热搜,不愧是程序员!
    零基础想要转行成为程序员?这几点你要知道
    恩怨纠缠的苹果和微信!苹果底层开源代码中包含兼容微信的代码,这是苹果偷学代码啦?
    好你个C语言,原来还有这么多副面孔!
    “熊孩子”乱敲键盘就攻破了Linux桌面,其父亲发现linux漏洞
  • 原文地址:https://www.cnblogs.com/sunfuyou/p/6883340.html
Copyright © 2011-2022 走看看