美文网首页
iOS-UIImagePickerController所有API

iOS-UIImagePickerController所有API

作者: Imkata | 来源:发表于2021-04-18 23:38 被阅读0次

    UIImagePickerController是系统的图片选择器,可以拍照和选择照片,只能选单张图片,而且选择完可以裁剪正方形,所以一般用来设置头像。

    @class UIImage;
    @protocol UIImagePickerControllerDelegate; //图片选择的代理
    
    //UIImagePickerController的源类型
    typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
        UIImagePickerControllerSourceTypePhotoLibrary //相册
        UIImagePickerControllerSourceTypeCamera, //相机
        UIImagePickerControllerSourceTypeSavedPhotosAlbum //照片库
    };
    
    //拍摄视频质量
    typedef NS_ENUM(NSInteger, UIImagePickerControllerQualityType) {
        UIImagePickerControllerQualityTypeHigh = 0,       // 高
        UIImagePickerControllerQualityTypeMedium = 1,     // 中等,适合wifi下
        UIImagePickerControllerQualityTypeLow = 2,         // 低等,适合流量
        UIImagePickerControllerQualityType640x480 // VGA quality
        UIImagePickerControllerQualityTypeIFrame1280x720
        UIImagePickerControllerQualityTypeIFrame960x540
    } API_UNAVAILABLE(tvos);
    
    //相机模式
    typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraCaptureMode) {
        UIImagePickerControllerCameraCaptureModePhoto, //拍照
        UIImagePickerControllerCameraCaptureModeVideo //拍视频
    } API_UNAVAILABLE(tvos);
    
    //相机摄像头
    typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraDevice) {
        UIImagePickerControllerCameraDeviceRear, //后置摄像头
        UIImagePickerControllerCameraDeviceFront //前置摄像头
    } API_UNAVAILABLE(tvos);
    
    //闪光灯模式
    typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraFlashMode) {
        UIImagePickerControllerCameraFlashModeOff  = -1, //关闭
        UIImagePickerControllerCameraFlashModeAuto = 0, //自动,默认
        UIImagePickerControllerCameraFlashModeOn   = 1 //打开
    } API_UNAVAILABLE(tvos);
    
    //图片出口预设
    typedef NS_ENUM(NSInteger, UIImagePickerControllerImageURLExportPreset) {
        UIImagePickerControllerImageURLExportPresetCompatible = 0, //兼容
        UIImagePickerControllerImageURLExportPresetCurrent //当前
    };
    
    //获取图片的InfoKey
    typedef NSString * UIImagePickerControllerInfoKey;
    
    UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerMediaType //当前媒体类型,比如kUTTypeImage
    UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerOriginalImage //获取原图
    UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerEditedImage //编辑后的照片
    UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerCropRect //裁剪的范围,NSValue (CGRect)
    UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerMediaURL //媒体资源的URL
    UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerReferenceURL // 一个引用assetlibrary框架中的资产的NSURL
    UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerMediaMetadata // 一个NSDictionary,包含来自捕获照片的元数据
    UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerLivePhoto// 一个PHLivePhoto
    UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerPHAsset // 一个PHAsset
    UIKIT_EXTERN UIImagePickerControllerInfoKey const UIImagePickerControllerImageURL ;// 一个图片的URL
    
    //UIImagePickerController继承于UINavigationController
    @interface UIImagePickerController : UINavigationController <NSCoding>
    
    //sourceType是否可用
    + (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType;
    //sourceType对应下的,可用的媒体类型,比如kUTTypeImage
    + (nullable NSArray<NSString *> *)availableMediaTypesForSourceType:(UIImagePickerControllerSourceType)sourceType;
    //判断相机是否可用
    + (BOOL)isCameraDeviceAvailable:(UIImagePickerControllerCameraDevice)cameraDevice                   API_AVAILABLE(ios(4.0));
    //判断闪光灯是否支持
    + (BOOL)isFlashAvailableForCameraDevice:(UIImagePickerControllerCameraDevice)cameraDevice;
    //返回可用的相机模式 [拍照、视频];
    + (nullable NSArray<NSNumber *> *)availableCaptureModesForCameraDevice:(UIImagePickerControllerCameraDevice)cameraDevice API_AVAILABLE(ios(4.0));
    
    //设置图片选择器的代理,需要遵守UIImagePickerControllerDelegate和UINavigationControllerDelegate代理
    //UIImagePickerControllerDelegate 通过此代理获取选择后的图片
    //UINavigationControllerDelegate 因为图片选择器继承于导航控制器,所以需要遵守此代理做一些和导航相关的操作
    @property(nullable,nonatomic,weak)      id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;
    //设置UIImagePickerController的源类型,默认是UIImagePickerControllerSourceTypePhotoLibrary
    @property(nonatomic)           UIImagePickerControllerSourceType     sourceType;
    //设置支持的媒体类型,默认是一个包含kUTTypeImage的数组
    @property(nonatomic,copy)      NSArray<NSString *>                   *mediaTypes;
    //设置是否允许编辑,默认否
    @property(nonatomic)           BOOL                                  allowsEditing;
    //设置图片输出预设 默认是UIImagePickerControllerImageExportPresetCompatible兼容
    @property(nonatomic)           UIImagePickerControllerImageURLExportPreset imageExportPreset;
    //相机:拍视频的最大时间
    //相册:选择视频的最大时间(大于此时间的视频不会显示)
    //默认10秒
    @property(nonatomic)           NSTimeInterval                        videoMaximumDuration;
    //拍摄视频质量,默认UIImagePickerControllerQualityTypeMedium中等
    @property(nonatomic)           UIImagePickerControllerQualityType    videoQuality;
    //设置视频输出预设
    //videoExportPreset可以用来指定转码质量的视频(通过AVAssetExportPreset*字符串)。如果该值为nil(默认值),则transcodeQuality将由视频质量决定。如果源类型是UIImagePickerControllerSourceTypeCamera则无效
    @property(nonatomic, copy)     NSString                              *videoExportPreset;
    //是否显示相机下面的工具栏,默认YES,设置为NO的时候可以自定义
    @property(nonatomic)           BOOL                                  showsCameraControls;
    //设置摄像机界面view上面盖的一层view
    @property(nullable, nonatomic,strong) __kindof UIView                *cameraOverlayView;
    //设置摄像机界面view的transform属性,可以旋转、缩放等
    @property(nonatomic)           CGAffineTransform                     cameraViewTransform;
    
    //编程的方式拍照
    - (void)takePicture;
    //编程的方式拍视频:开始录像
    - (BOOL)startVideoCapture;
    //编程的方式拍视频:结束录像
    - (void)stopVideoCapture;
    
    //设置相机模式,拍照或者录像:默认是拍照
    @property(nonatomic) UIImagePickerControllerCameraCaptureMode cameraCaptureMode;
    //设置默认启用的摄像头,默认是后置摄像头
    @property(nonatomic) UIImagePickerControllerCameraDevice      cameraDevice;
    //设置闪光灯模式(拍照和录像都可以设置)
    @property(nonatomic) UIImagePickerControllerCameraFlashMode   cameraFlashMode;
    
    @end
    
    //图片选择的代理
    @protocol UIImagePickerControllerDelegate<NSObject>
    @optional
    
    /// 选择照片之后的回调,选择器不会自动dismiss,需要手动dismiss
    /// @param picker 图片选择器
    /// @param editingInfo 编辑后的图片info字典,图片的信息在这里面,通过UIImagePickerControllerInfoKey获取
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info;
    //点击了取消按钮
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
    
    @end
    
    //添加一个图片到相册,回调函数是 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;格式的
    UIKIT_EXTERN void UIImageWriteToSavedPhotosAlbum(UIImage *image, __nullable id completionTarget, __nullable SEL completionSelector, void * __nullable contextInfo);
    
    //某个视频是否可以保存到已保存的相册中
    UIKIT_EXTERN BOOL UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(NSString *videoPath) API_AVAILABLE(ios(3.1)) API_UNAVAILABLE(tvos);
    
    //添加一个视频到相册,回调函数是- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;格式的
    UIKIT_EXTERN void UISaveVideoAtPathToSavedPhotosAlbum(NSString *videoPath, __nullable id completionTarget, __nullable SEL completionSelector, void * __nullable contextInfo) API_AVAILABLE(ios(3.1)) API_UNAVAILABLE(tvos);
    

    相关文章

      网友评论

          本文标题:iOS-UIImagePickerController所有API

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