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

    }

  • 相关阅读:
    SpringIOC的小例子
    java中递归实现复制多级文件夹
    快速排序和几种简单排序
    Oracle面试的基本题
    多态的两个小例子
    单例模式
    内部类与匿名内部类
    C#
    C#
    C#
  • 原文地址:https://www.cnblogs.com/sunfuyou/p/6883340.html
Copyright © 2011-2022 走看看