UIImagePickerController是系統封裝好的一個導航視圖控制器,使用其開發者能夠十分方便的進行相機相冊相關功能的調用。UIImagePickerController繼承於UINavigationController,其經過代理的方式將用戶獲取的圖片或者視頻文件傳入給開發者。數組
在使用UIImagePickerController以前,應該先判斷設備作支持的媒體文件獲取類型,使用以下方法進行判斷:框架
//判斷是否支持某個數據提供類型 /* UIImagePickerControllerSourceType枚舉定義以下: typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) { //系統圖庫 UIImagePickerControllerSourceTypePhotoLibrary, //相機 UIImagePickerControllerSourceTypeCamera, //系統相冊 UIImagePickerControllerSourceTypeSavedPhotosAlbum } __TVOS_PROHIBITED; */ + (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType; //判斷某個數據提供者所支持的文件格式 /* 文件格式定義在<MobileCoreServices/MobileCoreServices.h>框架中 */ + (nullable NSArray<NSString *> *)availableMediaTypesForSourceType:(UIImagePickerControllerSourceType)sourceType; //判斷所支持的相機設備 /* typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraDevice) { //前置攝像頭 UIImagePickerControllerCameraDeviceRear, //後置攝像頭 UIImagePickerControllerCameraDeviceFront } __TVOS_PROHIBITED; */ + (BOOL)isCameraDeviceAvailable:(UIImagePickerControllerCameraDevice)cameraDevice NS_AVAILABLE_IOS(4_0); //判斷對閃光燈的支持 + (BOOL)isFlashAvailableForCameraDevice:(UIImagePickerControllerCameraDevice)cameraDevice NS_AVAILABLE_IOS(4_0); //判斷相機設備支持的媒體模式 /* 返回值爲以下枚舉: typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraCaptureMode) { //照片模式 UIImagePickerControllerCameraCaptureModePhoto, //視頻模式 UIImagePickerControllerCameraCaptureModeVideo } __TVOS_PROHIBITED; */ + (nullable NSArray<NSNumber *> *)availableCaptureModesForCameraDevice:(UIImagePickerControllerCameraDevice)cameraDevice NS_AVAILABLE_IOS(4_0);
上面提到的定義於<MobileCoreServices/MobileCoreServices.h>框架中的文件類型,列舉以下:ide
//圖片類型 extern const CFStringRef kUTTypeImage __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); //JPEG格式 extern const CFStringRef kUTTypeJPEG __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); //JPEG2000格式 extern const CFStringRef kUTTypeJPEG2000 __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); extern const CFStringRef kUTTypeTIFF __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); extern const CFStringRef kUTTypePICT __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); extern const CFStringRef kUTTypeGIF __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); extern const CFStringRef kUTTypePNG __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); extern const CFStringRef kUTTypeQuickTimeImage __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); extern const CFStringRef kUTTypeAppleICNS __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); extern const CFStringRef kUTTypeBMP __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); extern const CFStringRef kUTTypeICO __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); extern const CFStringRef kUTTypeRawImage __OSX_AVAILABLE_STARTING(__MAC_10_10,__IPHONE_8_0); extern const CFStringRef kUTTypeScalableVectorGraphics __OSX_AVAILABLE_STARTING(__MAC_10_10,__IPHONE_8_0); extern const CFStringRef kUTTypeLivePhoto __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_9_1); //視頻格式 extern const CFStringRef kUTTypeAudiovisualContent __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); extern const CFStringRef kUTTypeMovie __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); extern const CFStringRef kUTTypeVideo __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); extern const CFStringRef kUTTypeAudio __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); extern const CFStringRef kUTTypeQuickTimeMovie __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); extern const CFStringRef kUTTypeMPEG __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); extern const CFStringRef kUTTypeMPEG2Video __OSX_AVAILABLE_STARTING(__MAC_10_10,__IPHONE_8_0); extern const CFStringRef kUTTypeMPEG2TransportStream __OSX_AVAILABLE_STARTING(__MAC_10_10,__IPHONE_8_0); extern const CFStringRef kUTTypeMP3 __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); extern const CFStringRef kUTTypeMPEG4 __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); extern const CFStringRef kUTTypeMPEG4Audio __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); extern const CFStringRef kUTTypeAppleProtectedMPEG4Audio __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); extern const CFStringRef kUTTypeAppleProtectedMPEG4Video __OSX_AVAILABLE_STARTING(__MAC_10_10,__IPHONE_8_0); extern const CFStringRef kUTTypeAVIMovie __OSX_AVAILABLE_STARTING(__MAC_10_10,__IPHONE_8_0); extern const CFStringRef kUTTypeAudioInterchangeFileFormat __OSX_AVAILABLE_STARTING(__MAC_10_10,__IPHONE_8_0); extern const CFStringRef kUTTypeWaveformAudio __OSX_AVAILABLE_STARTING(__MAC_10_10,__IPHONE_8_0); extern const CFStringRef kUTTypeMIDIAudio __OSX_AVAILABLE_STARTING(__MAC_10_10,__IPHONE_8_0);
CFStringRef與NSString類型的轉換,能夠使用以下方法:ui
NSString * str = (__bridge NSString*)kUTTypeMovie;
UIImagePickerController中更多屬性與方法解析以下:atom
//設置代理 @property(nullable,nonatomic,weak) id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate; //設置書體提供者類型 默認爲圖庫 @property(nonatomic) UIImagePickerControllerSourceType sourceType; //設置所須要的數據類型,須要設置爲系統定義的文件類型字符串數組 默認爲kUTTypeImage @property(nonatomic,copy) NSArray<NSString *> *mediaTypes; //設置是否容許編輯圖片 設置爲YES,則用戶選擇圖片時能夠編輯裁剪圖片 @property(nonatomic) BOOL allowsEditing; //設置媒體文件的最大時長 默認爲10分鐘 @property(nonatomic) NSTimeInterval videoMaximumDuration; //設置媒體文件的質量 枚舉以下: /* typedef NS_ENUM(NSInteger, UIImagePickerControllerQualityType) { UIImagePickerControllerQualityTypeHigh = 0, // 高質量 UIImagePickerControllerQualityTypeMedium = 1, // 中等質量 UIImagePickerControllerQualityTypeLow = 2, // 低質量 UIImagePickerControllerQualityType640x480 NS_ENUM_AVAILABLE_IOS(4_0) = 3, UIImagePickerControllerQualityTypeIFrame1280x720 NS_ENUM_AVAILABLE_IOS(5_0) = 4, UIImagePickerControllerQualityTypeIFrame960x540 NS_ENUM_AVAILABLE_IOS(5_0) = 5, } __TVOS_PROHIBITED; */ @property(nonatomic) UIImagePickerControllerQualityType videoQuality; //設置是否顯示相機控制界面 @property(nonatomic) BOOL showsCameraControls; //自定義的拍照界面 其會覆蓋在原拍照界面上 @property(nullable, nonatomic,strong) __kindof UIView *cameraOverlayView NS_AVAILABLE_IOS(3_1); //設置拍照界面的transform @property(nonatomic) CGAffineTransform cameraViewTransform ; //拍照 - (void)takePicture NS_AVAILABLE_IOS(3_1); //進行視頻捕獲 - (BOOL)startVideoCapture NS_AVAILABLE_IOS(4_0); //中止視頻捕獲 - (void)stopVideoCapture NS_AVAILABLE_IOS(4_0); //設置相機捕獲模式 照片或視頻 @property(nonatomic) UIImagePickerControllerCameraCaptureMode cameraCaptureMode; //設置相機設備 前置或後置攝像頭 @property(nonatomic) UIImagePickerControllerCameraDevice cameraDevice; //設置閃光燈模式 /* typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraFlashMode) { UIImagePickerControllerCameraFlashModeOff = -1, //關閉 UIImagePickerControllerCameraFlashModeAuto = 0, //自動 UIImagePickerControllerCameraFlashModeOn = 1 //開啓 } __TVOS_PROHIBITED; */ @property(nonatomic) UIImagePickerControllerCameraFlashMode cameraFlashMode;
//相機拍照完成或者從圖庫相冊選擇相片完成後觸發的回調方法 editingInfo字典中將傳入編輯信息 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo NS_DEPRECATED_IOS(2_0, 3_0); //相機錄像或者從圖庫相冊選擇視頻完成後觸發的回調方法 info字典中是具體信息 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info; //ImagePickerController取消選擇是回調的方法 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
上面方法中的編輯字典與信息字典中,約定好了一些鍵值對,開發者能夠經過相應的鍵獲取須要的信息,規則以下:spa
//這個鍵對應NSString類型的值 意義爲媒體文件的格式 UIKIT_EXTERN NSString *const UIImagePickerControllerMediaType; //這個鍵對應UIImage類型的值 意義爲獲取的原始圖片 UIKIT_EXTERN NSString *const UIImagePickerControllerOriginalImage; //這個件對應UIIImage類型的值 意義爲獲取編輯後的圖片 UIKIT_EXTERN NSString *const UIImagePickerControllerEditedImage; //這個鍵對應一個NSValue值 能夠轉爲CGRect類型 意義爲編輯的圖片範圍 UIKIT_EXTERN NSString *const UIImagePickerControllerCropRect; //這個鍵對應媒體文件的URL UIKIT_EXTERN NSString *const UIImagePickerControllerMediaURL; //這個鍵對應圖庫中的URL UIKIT_EXTERN NSString *const UIImagePickerControllerReferenceURL; //這個鍵對應一個NSDictionary 裏面存放媒體數據 UIKIT_EXTERN NSString *const UIImagePickerControllerMediaMetadata; //現場圖片數據 相機捕捉圖片時會記錄聲音 UIKIT_EXTERN NSString *const UIImagePickerControllerLivePhoto;
系統也提供了對相機照片和視頻進行存儲的方式,列舉以下:代理
//將圖片數據存儲到相冊 void UIImageWriteToSavedPhotosAlbum(UIImage *image, __nullable id completionTarget, __nullable SEL completionSelector, void * __nullable contextInfo); //將視頻保存到相冊 BOOL UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(NSString *videoPath); void UISaveVideoAtPathToSavedPhotosAlbum(NSString *videoPath, __nullable id completionTarget, __nullable SEL completionSelector, void * __nullable contextInfo);
專一技術,熱愛生活,交流技術,也作朋友。code
——琿少 QQ羣:203317592orm