OCiOS开发:使用相册、照相机和录像

简介

UIImagePickerController是一个独立的控制器类,继承自UINavigationController,因此它拥有UINavigationController相同的功能,但我们无法将它放入到我们自己的导航控制器栈中,它作为一个模态视图单独运行在你的界面之上,提供少量的属性和方法供我们使用,因此我们无法改变它的行为,只能做些简单的选取图片以及照相机的使用。

UIImagePickerController共有三种 sourceType 可选:

  • UIImagePickerControllerSourceTypePhotoLibrary:所有你能通过iPhone内置的照片应用看得到的,通过这个源类型都能显示出来。

  • UIImagePickerControllerSourceTypeCamera:允许用户使用iPhone内置摄像头拍照。

  • UIImagePickerControllerSourceTypeSavedPhotosAlbum:包含用户通过摄像头拍摄的。

使用图像拾取器

使用图像拾取器需要遵守两个协议:<UINavigationControllerDelegate, UIImagePickerControllerDelegate>。

初始化、配置图像拾取器

// 选择相册照片
- (void)selectPhotoAlbumPhotos {
    // 获取支持的媒体格式
    NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    // 判断是否支持需要设置的sourceType
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

        // 1、设置图片拾取器上的sourceType
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        // 2、设置支持的媒体格式
        _imagePickerController.mediaTypes = @[mediaTypes[0]];
        // 3、其他设置
        _imagePickerController.allowsEditing = YES; // 如果设置为NO,当用户选择了图片之后不会进入图像编辑界面。
        // 4、推送图片拾取器控制器
        [self presentViewController:_imagePickerController animated:YES completion:nil];

    }
}

回调方法

用户选择

当用户选择了某一张图片或编辑使用了某张图片后会回调以下方法:

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

我们可通过上述方法中info参数得到有用的信息,按如下方式可获取字典的具体值:

UIImage *editedImage = info[@"UIImagePickerControllerEditedImage"];

下面列举5个常用的字典键值:

  • UIImagePickerControllerMediaType:用户选择的媒体类型,得到的是一个NSString得值,返回@"public.image"或者@"public.movie",通过这个值我们就可以判断用户选取的是图片还是视频了。

  • UIImagePickerControllerOriginalImage:没有被编辑过的原始图像。

  • UIImagePickerControllerEditedImage:用户编辑过后的图像(allowsEditing属性设为YES,通过编辑的到的图像)。

  • UIImagePickerControllerCropRect:返回用户选择的图像区域,它作为一个NSRect数据类型返回。

  • UIImagePickerControllerReferenceURL:返回一个媒体类型的NSURL

实例,获取选择的图片,赋值给图片视图,呈现在用户界面上。

// 用户选择了某个媒体
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    NSLog(@"User chosed imageView media with info '%@'.", info);

    _uploadButton.hidden = NO;

    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        if ([info[UIImagePickerControllerMediaType] isEqualToString:@"public.image"]) {
            UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
            _headPortraitImageView.image = originalImage;
        }
    }else if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary){
        // UIImage *editedImage = info[@"UIImagePickerControllerEditedImage"];
        // _headPortraitImageView.image = editedImage;

        _headPortraitImageView.image = info[UIImagePickerControllerEditedImage];
    }

    [self dismissViewControllerAnimated:YES completion:nil];
}
用户取消

当用户点击取消按钮的时候会调用下面的方法:

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;

用户选取了图片或者点击了取消以后,我们还需要添加退出图像拾取器的代码,否则将永远处于图像拾取器界面。需要添加的代码如下所示:

// 用户点击了取消按钮
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}

使用照相机

要使用照相机就必须在真机环境下运行,模拟器暂不支持该功能,如果你在模拟器上运行照相机将会直接奔溃。使用照相机很简单,只需将sourceType更改为UIImagePickerControllerSourceTypeCamera即可,别的代码都跟图像拾取器的使用基本一致。

使用照相机的时候,我们可以使用默认的照相机界面,也可以完全使用自己定制的界面。要自定义界面,我们需要将showsCameraControls属性置为NO;然后将自定义的UIView赋给cameraOverlayView属性即可。

照相机常用属性

  • cameraCaptureMode:设置相机模式

    • UIImagePickerControllerCameraCaptureModePhoto:拍照
    • UIImagePickerControllerCameraCaptureModeVideo:录制
  • cameraDevice:更改摄像头

    • UIImagePickerControllerCameraDeviceFront:前置摄像头
    • UIImagePickerControllerCameraDeviceRear :后置摄像头
  • cameraFlashMode:设置闪关灯模式

    • UIImagePickerControllerCameraFlashModeOff:关闭闪关灯
    • UIImagePickerControllerCameraFlashModeAuto:自动模式
    • UIImagePickerControllerCameraFlashModeOn:打开闪关灯
代码示例
// 拍照
- (void)takingPictures {
    // 获取支持的媒体格式
    NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];

    // 判断是否支持需要设置的sourceType
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        // 1、设置图片拾取器上的sourceType
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

        // 2、设置支持的媒体格式
        _imagePickerController.mediaTypes = @[mediaTypes[0]];
        // 3、其他设置
        // 设置相机模式
        _imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        // 设置摄像头:前置/后置
        _imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        // 设置闪光模式
        _imagePickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;


        // 4、推送图片拾取器控制器
        [self presentViewController:_imagePickerController animated:YES completion:nil];

    }else {
        NSLog(@"当前设备不支持拍照");
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示"
                                                                                  message:@"当前设备不支持拍照"
                                                                           preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定"
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction *action) {
                                                              _uploadButton.hidden = NO;
                                                          }]];
        [self presentViewController:alertController
                           animated:YES
                         completion:nil];
    }
}

使用视频录制

  • 使用视频录制和拍照用法基本一致,只需将 cameraCaptureMode 属性设为 UIImagePickerControllerCameraCaptureModeVideo 即可,代码如下:

代码示例

// 录制
- (void)takingShooting {
    // 获取支持的媒体格式
    NSArray * mediaTypes =[UIImagePickerController  availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
    // 判断是否支持需要设置的sourceType
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        _imagePickerController.mediaTypes = @[mediaTypes[1]];
        _imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
        [self presentViewController:_imagePickerController animated:YES completion:nil];
    }else {
        NSLog(@"当前设备不支持录像");
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示"
                                                                                  message:@"当前设备不支持录像"
                                                                           preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定"
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction *action) {
                                                              _uploadButton.hidden = NO;
                                                          }]];
        [self presentViewController:alertController
                           animated:YES
                         completion:nil];
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值