iOS高级功能实现

自定义UIImagecontroller实践 2022-04-0

2022-04-11  本文已影响0人  勇往直前888

需求

在拍照过程中检测手机是否水平。水平的时候允许拍照,倾斜的时候不能拍照。

思路

UIImagecontroller简介

typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
    UIImagePickerControllerSourceTypePhotoLibrary API_DEPRECATED("Will be removed in a future release, use PHPicker.", ios(2, API_TO_BE_DEPRECATED)),
    UIImagePickerControllerSourceTypeCamera,
    UIImagePickerControllerSourceTypeSavedPhotosAlbum API_DEPRECATED("Will be removed in a future release, use PHPicker.", ios(2, API_TO_BE_DEPRECATED)),
} API_UNAVAILABLE(tvos);

不是所有的设备都支持相机。由于UINavigationController还支持照片库,所以设置sourceType是首要任务。下面的方法和属性配合起来用比较好。

+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType;                 // returns YES if source is available (i.e. camera present)

@property(nonatomic)           UIImagePickerControllerSourceType     sourceType;                                                        // default value is UIImagePickerControllerSourceTypePhotoLibrary.
typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraCaptureMode) {
    UIImagePickerControllerCameraCaptureModePhoto,
    UIImagePickerControllerCameraCaptureModeVideo
} API_UNAVAILABLE(tvos);
  • @property(nonatomic,copy) NSArray<NSString *> *mediaTypes; // default value is an array containing kUTTypeImage.
    这个属性设置支持那些,默认只有照片;如果要照片和视频都支持,需要设置为@[@"public.image", @"public.movie"]
  • @property(nonatomic) UIImagePickerControllerCameraCaptureMode cameraCaptureMode API_AVAILABLE(ios(4.0)); // default is UIImagePickerControllerCameraCaptureModePhoto
    这个属性设置当前是拍照片还是拍视频,默认是拍照片
@property(nonatomic)           BOOL                                  allowsEditing API_AVAILABLE(ios(3.1));     // replacement for -allowsImageEditing; default value is NO.
typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraDevice) {
    UIImagePickerControllerCameraDeviceRear,
    UIImagePickerControllerCameraDeviceFront
} API_UNAVAILABLE(tvos);

@property(nonatomic) UIImagePickerControllerCameraDevice      cameraDevice      API_AVAILABLE(ios(4.0)); // default is UIImagePickerControllerCameraDeviceRear
typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraFlashMode) {
    UIImagePickerControllerCameraFlashModeOff  = -1,
    UIImagePickerControllerCameraFlashModeAuto = 0,
    UIImagePickerControllerCameraFlashModeOn   = 1
} API_UNAVAILABLE(tvos);

@property(nonatomic) UIImagePickerControllerCameraFlashMode   cameraFlashMode   API_AVAILABLE(ios(4.0)); // default is UIImagePickerControllerCameraFlashModeAuto. 
// camera additions available only if sourceType is UIImagePickerControllerSourceTypeCamera.
@property(nonatomic)           BOOL                                  showsCameraControls API_AVAILABLE(ios(3.1));   // set to NO to hide all standard camera UI. default is YES
@property(nullable, nonatomic,strong) __kindof UIView                *cameraOverlayView  API_AVAILABLE(ios(3.1));   // set a view to overlay the preview view.
@property(nonatomic)           CGAffineTransform                     cameraViewTransform API_AVAILABLE(ios(3.1));   // set the transform of the preview view.

自定义图层

        // 不用系统提供的功能按钮,采用自定义的视图来做控制
        self.showsCameraControls = NO;
        self.cameraOverlayView = self.customerView;
- (void)takePicture API_AVAILABLE(ios(3.1));                                                   
// programmatically initiates still image capture. ignored if image capture is in-flight.
// clients can initiate additional captures after receiving -imagePickerController:didFinishPickingMediaWithInfo: delegate callback

小结

本来的想法是采用在自定义图层cameraOverlayView上添加操作按钮来实现需求。但是体验下来,还不如直接采用默认的按钮来做效果更好,代码也更简洁。

至于要更多地控制相机的细节,咨询过大神,回应说应该用更底层API,比如AVCaptureSession之类的。

参考文章

iOS UIImagePickerController轻松调用相机详细介绍

iOS开发系列--音频播放、录音、视频播放、拍照、视频录制

iOS设备的重力感应

iOS 重力感应 学习1 陀螺仪 水平仪 指南针

上一篇下一篇

猜你喜欢

热点阅读