UIImagePickerController和UIAlertController结合使用

在处理个人资料 - 头像的时候,通常有两个选项,一个是调用系统相机,一个是调用系统相册。这里要使用的就是UIImagePickerController方法。

在头像位置的imageView添加一个手势,或者添加一个透明的按钮,用来实现click方法,直接上代码:

- (IBAction)click:(id)sender{

    //创建提醒视图

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提醒" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

         //判断设备是否存在摄像头,有就调用系统相机,没有,就提醒用户

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

            //创建相机

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

            //文件由来

            picker.sourceType = UIImagePickerControllerSourceTypeCamera; //指定数据来源来自于相机

            picker.delegate  = self;// 指定代理

            picker.allowsEditing = YES; //允许编辑

            //模态弹出

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

        }else{

            //没有摄像头,提醒用户 您的设备没有摄像头

            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"您的设备没有摄像头" message:nil preferredStyle:UIAlertControllerStyleAlert];

            UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];

            [alertController addAction:alertAction1];

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

        }

    }];

    [alertController addAction:alertAction];

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

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

        pickerC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//指定数据来源为相册

        pickerC.delegate = self;  //指定代理

        pickerC.allowsEditing = YES;  // 允许编辑

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

    }];

    [alertController addAction:alertAction2];

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

}

//选取图片之后执行的方法

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

    NSLog(@"%@",info);

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    self.photoImage.image = image;

    [picker dismissViewControllerAnimated:YES completion:nil];

}
时间: 2024-07-30 04:56:32

UIImagePickerController和UIAlertController结合使用的相关文章

swift2.0 UIImagePickerController 拍照 相册 录像

系统 ios9.1 语言swift2.0 在app 里最常用的功能就是多媒体选择,首先我们storyboard 创建一个button 用于触发选择事件 @IBAction func selectImageAction(sender: AnyObject) { } 这时候通常会弹出来一个ActionSheet 上面有拍照 , 相册,录像 和取消 这几项.iOS 8 以后actionsheet 和 alertview 都统一用UIAlertController 方法调用,8.3以前actionshe

iOS8中UIActionSheet弹出UIImagePickerController异常处理

iOS8之后,UIActionSheet改父于UIAlertController.带来了一丢丢兼容性的问题. 比如在弹出的actionsheet中选择从相册选择图片或者拍照,之后弹出UIImagePickerController进行选择. 在iOS8以前的方法里,直接在 -(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex; Click的委托事件里处理就好了,但是在

iOS8 iPad Warning: Attempt to present <UIImagePickerController:xxxx > on xxxx which is already presenting (null)

解决方法: /* I think this is because in iOS 8, alert views and action sheets are actually presented view controllers (UIAlertController). So, if you're presenting a new view controller in response to an action from the UIAlertView, it's being presented w

Swift详解UIImagePickerController调用相册相机功能

首先,添加UINavigationControllerDelegate和UIImagePickerControllerDelegate两项protocol. 使用UIImagePickerController,就必须实现UINavigationControllerDelegate这个protocol,因为调用过程中会出现NavigationBar,如果没实现,也不会说运行不了.只是Xcode会直接就给你一个warning. 直接上自己用swift写的一个设置头像的小demo,可直接复制使用.注释

iOS关于UITabView和UIAlertController,UIAlertAction以及UINavigation,值修改的传递页面推送

关于UITabView和UIAlertController,UIAlertAction以及UINavigation,值修改的传递 集合嵌套集合的操作 声明 两个必须的的代理 实现部分代码 - (void)viewDidLoad { [super viewDidLoad]; // 创建一个TabView self.tabv = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped]; sel

UIAlertController、UIAlertAction 警告框

NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertAction : NSObject <NSCopying> //创建操作 + (instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler; NS_CLASS_AVAIL

UIAlertController

UIAlertController * alert =[UIAlertController alertControllerWithTitle:@"新建相册" message:@"请输入新建相册的名字" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UI

使用UIImagePickerController时3DTouch引起的Crash问题的解决--备用

一.crash的场景 程序中用到UIImagePickerController时,如果在IPhone6S上运行APP,当forceTouch 一个图片时程序会crash,并附带如下crash message: * Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘+[NSObject previewingContext:viewControllerForLocation:]: unre

IOS开发之UIAlertView与UIAlertController的详尽用法说明

本文将从四个方面对IOS开发中UIAlertView与UIAlertController的用法进行讲解: 一.UIAlertView与UIAlertController是什么东东? 二.我们为什么要用UIAlertView或UIAlertController? 三.如何使用UIAlertView和UIAlertController? 四.阅读提醒. 一.UIAlertView与UIAlertController是什么东东? 一句话,在所有移动应用中,出现的提示窗一般都由UIAlertView或U