欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ios基础篇(一)—— UIImagePickerController类

程序员文章站 2022-04-14 08:41:39
UIImagePickerController简述:UIImagePickerController 类是获取选择图片和视频的用户接口,我们可以用UIImagePickerController选择我们所需要的图片和视频。注:UIImagePickerController不能够任意定制,也不可以继承生成子类。一、UIImagePickerController的属性:1、sourceType设置UIImagePicker照片源类型,默认有3种。照片源类型指定使用模式(照相机/相册/照片库)...

UIImagePickerController简述:

UIImagePickerController 类是获取选择图片和视频的用户接口,我们可以用UIImagePickerController选择我们所需要的图片和视频。

注:UIImagePickerController不能够任意定制,也不可以继承生成子类。

一、UIImagePickerController的属性:

1、sourceType

设置UIImagePicker照片源类型,默认有3种。照片源类型

指定使用模式(照相机/相册/照片库)

enum {
UIImagePickerControllerSourceTypePhotoLibrary,  //照片库(通过同步存放的,用户不能删除)
UIImagePickerControllerSourceTypeCamera,  //照相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum //保存的照片(通过拍照或者截屏保存的,用户可以删除)
};

2、BOOL showsCameraControls

设置拍照时的下方的工具栏是否显示,如果需要自定义拍摄界面,则可把该工具栏隐藏。

3. videoMaximumDuration

设置UIImagePicker的最大视频持续时间.默认10分钟

4. + availableMediaTypesForSourceType: // 指定源可用的媒体种类

 //获得相机模式下支持的媒体类型

NSArray* availableMediaTypes =
[UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];

3、BOOL allowsEditing

设置当拍照完或在相册选完照片后,是否跳到编辑模式进行图片剪裁即是否允许编辑

showsCameraControls=Yes时才有效果。

[imagePicker setAllowsEditing:YES]; 

4、cameraDevice

使用的镜头(默认后置的)

判断设备是否支持前置摄像头/后置摄像头

enum {
     UIImagePickerControllerCameraDeviceRear,
     UIImagePickerControllerCameraDeviceFront
     };

+ isCameraDeviceAvailable: // 摄像设备是否可用.
+ availableCaptureModesForCameraDevice: // 设备可用的选取模式 

  cameraCaptureMode //相机捕获模式

5、cameraFlashMode

设置闪光灯模式  (默认自动)

 enum {
     UIImagePickerControllerCameraFlashModeOff  = -1,
     UIImagePickerControllerCameraFlashModeAuto = 0,
     UIImagePickerControllerCameraFlashModeOn   = 1
     };
 + isFlashAvailableForCameraDevice: // 是否有闪光能力

6、mediaTypes

设置相机支持的类型,拍照和录像

+ (NSArray *)availableMediaTypesForSourceType:(UIImagePickerControllerSourceType)sourceType
  • UIImageWriteToSavedPhotosAlbum保存图像,
  • UISaveVideoAtPathToSavedPhotosAlbum保存视频.
  • 4.0后使用writeImageToSavedPhotosAlbum:metadata:completionBlock:保存元数据.

一共有三个可选的代理方法UIImagePickerControllerDelegate 

– imagePickerController:didFinishPickingMediaWithInfo:  
– imagePickerControllerDidCancel:  
– imagePickerController:didFinishPickingImage:editingInfo: 
- (void)imagePickerController:(UIImagePickerController *)picker 
didFinishPickingMediaWithInfo:(NSDictionary *)info

info中包括选取的照片,视频的主要信息

NSString *const UIImagePickerControllerMediaType;       选取的类型 public.image  public.movie
NSString *const UIImagePickerControllerOriginalImage;    修改前的UIImage object.
NSString *const UIImagePickerControllerEditedImage;    修改后的UIImage object.
NSString *const UIImagePickerControllerCropRect;原始图片的尺寸NSValue object containing a CGRect data type
NSString *const UIImagePickerControllerMediaURL;     视频在文件系统中 的 NSURL地址

7、cameraViewTransform

设置拍摄时屏幕的view的transform属性,可以实现旋转,缩放功能

enum{

CGAffineTransformMakeRotation;

CGAffineTransformMakeScale;

}

二、UIImagePickerController回调方法:

1、成功获得相片或视频后的回调

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

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    //通过UIImagePickerControllerMediaType判断返回的是照片还是视频
      NSString* type = [info objectForKey:UIImagePickerControllerMediaType];
      //如果返回的type等于kUTTypeImage,代表返回的是照片,并且需要判断当前相机使用的sourcetype是拍照还是相册
      if ([type isEqualToString:(NSString*)kUTTypeImage]&&picker.sourceType==UIImagePickerControllerSourceTypeCamera) {
          //获取照片的原图
          UIImage* original = [info
  objectForKey:UIImagePickerControllerOriginalImage];
          //获取图片裁剪的图
         UIImage* edit = [info objectForKey:UIImagePickerControllerEditedImage];
         //获取图片裁剪后,剩下的图
         UIImage* crop = [info objectForKey:UIImagePickerControllerCropRect];
         //获取图片的url
         NSURL* url = [info objectForKey:UIImagePickerControllerMediaURL];
        //获取图片的metaData数据信息
         NSDictionary* metaData = [info objectForKey:UIImagePickerControllerMediaMetadata];
         //如果是拍照的照片,则需要手动保存到本地,系统不会自动保存拍照成功后的照片
         UIImageWriteToSavedPhotosAlbum(edit, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
     }else{
     
     }
     //模态方式退出uiimagepickercontroller
     [imagepicker dismissModalViewControllerAnimated:YES];
 }

2、取消照相机的回调

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

 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
     //模态方式退出UIImagePickerController
    [imagePicker dismissModalViewControllerAnimated:YES];
}

UIImagePickerController小例子

UIImagePickerController的代理需要遵守这两个协议.<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

#pragma mark 选择照片

- (void)selectPhoto{

    // 1. 首先判断照片源是否可用
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        // 0)实例化控制器
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        // 1)设置照片源
        [picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        // 2) 设置允许修改
        [picker setAllowsEditing:YES];
        // 3) 设置代理
        [picker setDelegate:self];
        // 4) 显示控制器
        [self presentViewController:picker animated:YES completion:nil];

    } else {
        NSLog(@"照片源不可用");
    }
}

#pragma mark - imagePicker代理方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image = info[@"UIImagePickerControllerEditedImage"];
    [_imageButton setImage:image forState:UIControlStateNormal];    
    // 关闭照片选择器
    [self dismissViewControllerAnimated:YES completion:nil];    
    // 需要将照片保存至应用程序沙箱,由于涉及到数据存储,同时与界面无关
    // 可以使用多线程来保存图像
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{     
        // 保存图像
        // 1. 取图像路径
        NSArray *docs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *imagePath = [docs[0]stringByAppendingPathComponent:@"abc.png"];
       // 2. 转换成NSData保存
        NSData *imageData = UIImagePNGRepresentation(image);
        [imageData writeToFile:imagePath atomically:YES];
    });
}

UIImagePicker使用步骤:

  1.     检查指定源是否可用. isSourceTypeAvailable:方法.
  2.     检查可用媒体(视频还是只能是图片) availableMediaTypesForSourceType:方法.
  3.     设置界面媒体属性mediaTypes property.
  4.     显示界面使用presentViewController:animated:completion:方法.iPad中是popover形式.需要确保sourceType有效.
  5.     相关操作,移除视图.


如果想创建一个完全自定义界面的image picker来浏览图片,使用 Assets Library Framework Reference中的类. (AV Foundation Programming Guide 中的 “Media Capture and Access to Camera” )

参考:https://www.jianshu.com/p/966a3b9a72be

本文地址:https://blog.csdn.net/qq_41431406/article/details/108975285

相关标签: ios小白