美文网首页
调取相机、相册

调取相机、相册

作者: fwlong | 来源:发表于2016-06-29 17:51 被阅读78次

点击图片调取相机或相册

//遵循协议(调取相册、相机使用)
<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;
      }

相关文章

  • 调取相机、相册

    点击图片调取相机或相册 选取完照片后要执行的代理方法 (void)imagePickerController:(U...

  • iOS中 读取相册,调用系统相机

    读取相册以及调取相机,将图片显示到imageView上 布局: 1.创建imageView 和 button 并为...

  • 调取系统相册 照相机

    - (IBAction)photo:(id)sender { UIImagePickerController* i...

  • vuejs2.0压缩图片插件localResizeIMG

    第一步: 第二步: 第三步: 扩展---调取相册,不限制图片格式,加上属性capture="camera" 调取相机

  • iOS关于相机相册权限设置

    iOS10之后,有一些童鞋提审应用时会出现因为没有对相机相册等权限的设置提醒而被拒绝,以及出现调取本地相册相机等出...

  • iOS关于相机相册权限设置

    iOS10出来之后,有一些童鞋提审应用时会出现因为没有对相机相册等权限的设置提醒而被拒绝,以及出现调取本地相册相机...

  • 如何封装调取系统相机与相册

    当我们在开发一款app时,经常会获取相机或者系统相册来上传头像,或者上传证件照等单张图片获取的需求,为了避免过多的...

  • iO10 调取本地相册,相机

    iOS 升级到10 以后,会出现调取本地相册,相机等出现崩溃的情况,其实,这是苹果为了安全而设置的权限所导致的,解...

  • 调取相机

    相片动辄5-6MB,所以存储在SQLite中是绝对不合适的,那就应该构建一个内容提供器,暴露相片访问接口,并将实体...

  • 适配Android7.0调取相机拍照并返回照片

    Android调取系统相机拍照获取到拍摄照片或从相册中直接选取照片后展示上传是Android开发中很常见的一个功能...

网友评论

      本文标题:调取相机、相册

      本文链接:https://www.haomeiwen.com/subject/cejxjttx.html