iOS通过UIAlertController弹出底部选择框来调用相机或者相册

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

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

            

        }];

        

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

            

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

            imagePicker.delegate = self;

            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){

                imagePicker.sourceTypeUIImagePickerControllerSourceTypeCamera;

            }

            [self presentViewController:imagePicker animated:YEScompletion:nil];

            

        }];

        

        UIAlertAction *picture = [UIAlertAction actionWithTitle:@"打开相册style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

            

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

            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

                pickerImage.sourceTypeUIImagePickerControllerSourceTypePhotoLibrary;

                pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];

                

            }

            pickerImage.delegate = self;

            pickerImage.allowsEditing = NO;

            

            [self presentViewController:pickerImage animated:YEScompletion:nil];

        }];

        [alertVc addAction:cancle];

        [alertVc addAction:camera];

        [alertVc addAction:picture];

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



// 上面写到Button的点击事件方法里,下面直接复制就OK,记得遵守代理协议

UIImagePickerControllerDelegate, UINavigationControllerDelegate



// 选择图片

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

{

    

    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];

    

    //当选择的类型是图片

    if ([type isEqualToString:@"public.image"])

    {

        //先把图片转成NSData

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

        NSData *data;

        if (UIImagePNGRepresentation(image) ==nil)

        {

            data = UIImageJPEGRepresentation(image,1.0);

        }

        else

        {

            data = UIImagePNGRepresentation(image);

        }

        

        //图片保存的路径

        //这里将图片放在沙盒的documents文件夹中

        NSString *DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

        

        //文件管理器

        NSFileManager *fileManager = [NSFileManager defaultManager];

        

        //把刚刚图片转换的data对象拷贝至沙盒中并保存为image.png

        [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];

        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/userHeader.png"contents:data attributes:nil];

        

        //关闭相册界面

        [picker dismissViewControllerAnimated:YEScompletion:nil];

        

        //加在视图中

        [self.mineView.headerButton setBackgroundImage:image forState:(UIControlStateNormal)];

        

    }

}

// 取消选取图片

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    [picker dismissViewControllerAnimated:YEScompletion:nil];

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值