美文网首页
iOS调用摄像头和相册选取图片

iOS调用摄像头和相册选取图片

作者: Maxoxo | 来源:发表于2019-08-09 23:55 被阅读0次

    iOS图片来源有三种方法:

    typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
        UIImagePickerControllerSourceTypePhotoLibrary,    //1.从图库中选择
        UIImagePickerControllerSourceTypeCamera,          //2.直接调用摄像头拍照
        UIImagePickerControllerSourceTypeSavedPhotosAlbum //3.从相册中选择
    } __TVOS_PROHIBITED;
    

    UIImagePickerController是系统提供的用来获取图片和视频的接口,用UIImagePickerController类来获取图片视频,大体分为以下几个步骤:

    1. 实例化UIImagePickerController
    2. 设置UIImagePickerController数据来源类型
    3. 设置代理,遵循UIImagePickerControllerDelegate,UINavigationControllerDelegate协议
    4. 实现代理方法接收选取图片并做处理
    

    在代理中获取我们选中的图片,UIImagePickerControllerDelegate代理中一共三个方法,其中一个3.0已经废弃了,只剩下两个我们需要用的。

    //1.当用户选取完成后调用;
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
     
    //2.当用户取消选取时调用;
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
    

    我们主要使用第一个代理方法,在选取图片完成后会调用,返回一个info字典。

    实际使用展示:

    @interface SelecteImageViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
    @property (nonatomic, strong) UIImagePickerController *picker;
    @property (nonatomic, strong) UIImageView *selectImageView;
    @end
    
    @implementation SelecteImageViewController
    
    - (void)imageViewDidClick:(UIImageView *)imageView ForIndex:(NSInteger)index {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        [alertController addAction:[UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [weakSelf goCameraViewController];
        }]];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [weakSelf directGoPhotoViewController];
        }]];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
    -(void)goCameraViewController {
        // 如果摄像头可用
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            //
            self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            self.picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
            self.picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        } else {
            [XWHUDManager showErrorTipHUD:@"无法打开摄像头"];
            return;
        }
        [self presentViewController:self.picker animated:YES completion:nil];
    }
    
    -(void)directGoPhotoViewController {
        self.picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        [self presentViewController:self.picker animated:YES completion:nil];
    }
    
    /**
     拍照或选择图片后的回调
    
     @param picker <#picker description#>
     @param info <#info description#>
     */
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
        FTLog(@"info--->成功:%@", info);
        // 获取用户拍摄的是照片还是视频
        NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
        // 判断获取类型:图片,并且是刚拍摄的照片
        if ([mediaType isEqualToString:(NSString *)kUTTypeImage] && picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
            
            UIImage *theImage = nil;
            // 判断,图片是否允许修改
            if ([picker allowsEditing]) {
                // 获取用户遍及之后的图像
                theImage = [info objectForKey:UIImagePickerControllerEditedImage];
            } else {
                // 获取原始图像
                theImage = [info objectForKey:UIImagePickerControllerOriginalImage];
            }
            // 保存图片到相册中
            UIImageWriteToSavedPhotosAlbum(theImage, self, nil, nil);
        }
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
    
    -(UIImagePickerController *)picker {
        if (!_picker) {
            _picker = [[UIImagePickerController alloc] init];
            _picker.delegate = self;
            _picker.editing = YES;
        }
        return _picker;
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:iOS调用摄像头和相册选取图片

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