调取相机、相册

点击图片调取相机或相册

//遵循协议(调取相册、相机使用)
<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
//给头像开启用户交互
 self.avatarImage.userInteractionEnabled = YES;
//设置手势识别
    UITapGestureRecognizer * recognize = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addDevies)];
    [self.avatarImage addGestureRecognizer:recognize];

//图片点击事件
- (void)addDevies
{
    //创建UIAlertController是为了让用户去选择照片来源,拍照或者相册.
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:0];
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    //相册
    UIAlertAction *albumAction = [UIAlertAction actionWithTitle:@"从相册选取" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
        
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        
        [self presentViewController:imagePickerController animated:YES completion:^{}];
    }];
    
    //相机
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"拍照" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
        
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        [self presentViewController:imagePickerController animated:YES completion:^{}];
    }];

    //取消
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction *action)
                                   {
                                       //这里可以不写代码
                                   }];
    [self presentViewController:alertController animated:YES completion:nil];
    
    //用来判断来源 Xcode中的模拟器是没有拍摄功能的,当用模拟器的时候我们不需要把拍照功能加速
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        
    {
        
        [alertController addAction:albumAction];
        [alertController addAction:cancelAction];
        [alertController addAction:photoAction];
        
    }
    
    else
    {
        [alertController addAction:albumAction];
        [alertController addAction:cancelAction];

        [alertController addAction:photoAction];
    }
    
}

选取完照片后要执行的代理方法

  • (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
    {
    [picker dismissViewControllerAnimated:YES completion:^{}];
    //选取裁剪后的图片
    UIImage image = [info objectForKey:UIImagePickerControllerEditedImage];
    /
    此处info 有六个值
    • UIImagePickerControllerMediaType; // an NSString UTTypeImage)
    • UIImagePickerControllerOriginalImage; // a UIImage 原始图片
    • UIImagePickerControllerEditedImage; // a UIImage 裁剪后图片
    • UIImagePickerControllerCropRect; // an NSValue (CGRect)
    • UIImagePickerControllerMediaURL; // an NSURL
    • UIImagePickerControllerReferenceURL // an NSURL that references an asset in the AssetsLibrary framework
    • UIImagePickerControllerMediaMetadata // an NSDictionary containing metadata from a captured photo
      */
      _avatarImage.image = image;
      }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • UIImagePickerController拍照与摄像 该类继承自UINavigationController类...
    小暖风阅读 1,097评论 3 3
  • { 11、核心动画 需要签协议,但是系统帮签好 一、CABasicAnimation 1、创建基础动画对象 CAB...
    CYC666阅读 1,591评论 2 4
  • 提到从摄像头/相册获取图片是面向终端用户的,由用户去浏览并选择图片为程序使用。在这里,我们需要过UIImagePi...
    Ashoka_APP阅读 1,532评论 1 1
  • 在IOS开发中经常会用到系统相册,相机,音视频等功能。每次都写比较麻烦,自己封装了一个工具类,实现一句话调用系统相...
    专注_刻意练习阅读 1,237评论 0 0
  • 昨天坐了七八个小时的车,来到江布拉克草原,远远的看着大片的麦田,视野开阔。预订的吉祥庄园就在景区游客服务中心对面,...
    春暖花开73801阅读 366评论 0 0