Summary of UIImagePickerController image picker in iOS

  • 2020-06-12 10:42:28
  • OfStack

UIImagePickerController is used to manage a customizable, system-supported user interface for capturing images and videos on the device. It can also be used to select stored images and videos in App. An UIImagePickerController manages user interactions and passes the results of those interactions to a proxy object. This class cannot be inherited or modified except for custom cameraOverlayView.

Let's take a look at some of UIImagePickerController's 1 properties briefly, and then code up.

1. Common attributes

(1) Type of selection interface shown by sourceType controller,

Contains three enumerated values


enum {
   UIImagePickerControllerSourceTypePhotoLibrary,      // Select an image or video from the gallery
   UIImagePickerControllerSourceTypeCamera,            // Used for taking photos or videos
   UIImagePickerControllerSourceTypeSavedPhotosAlbum   // Select an image or video from the album
};
typedef NSUInteger UIImagePickerControllerSourceType;

(2) mediaTypes

The default value is kUTTypeImage, meaning users can only select still images or shoot still images (relative to video)

When mediaTypes is set to kUTTypeImage, kUTTypeMovie (if supported by the device), you can choose to manipulate the video while manipulating the image.

When using KUTTypeImage,KUTTypeMovie, import #import < MobileCoreServices/MobileCoreServices.h >

(3) the allowEditing

Whether the user can modify the image or video, the default is NO

(3) the cameraOverlayView

At the top of the default image selection screen.

2. Basic usage

(1) Use isSourceTypeAvailable: to determine whether the current device supports SourceType

(2) If supported, use availableMediaTypesForSourceType to verify the supported mediaTypes under the current SourceType

(3) If mediaTypes(default is kUTTypeImage) needs to be adjusted, do so

(4) Display interface, using modal method to pop up.

(5) When the user completes the operation (cancel or selects a picture and a video), the agent will trigger the method, then close the interface and carry out relevant processing.

3. Details of the other two sourceType

The other two ways of USING sourceType are relatively simple. The situation when SourceType is UIImagePickerControllerSourceTypeCamera is explained in detail below.

Common attributes:

(1) videoQuality: It is used to set the quality of the video. The default value is UIImagePickerControllerQualityTypeMedium.

For video shooting and video selection, if you select an existing video and the video quality is higher than videoQuality set, the video will be converted to low quality

(What if there were video hell videoQuality)

(2) The maximum video time of videoMaximumDuration is 10 minutes by default

(3) the cameraViewTransform

The & # 8205; These properties are only available under camera

(4) cameraDevice


enum {
   UIImagePickerControllerCameraDeviceRear,  // Rear camera
   UIImagePickerControllerCameraDeviceFront  // Front facing camera
};
typedef NSUInteger UIImagePickerControllerCameraDevice;

(5) cameraCaptureMode

The mode you choose when you open the camera interface


enum {
   UIImagePickerControllerCameraCaptureModePhoto,  // Photo mode is used by default
   UIImagePickerControllerCameraCaptureModeVideo   // The default is camera mode
};
typedef NSUInteger UIImagePickerControllerCameraCaptureMode;

(6) cameraFlashMode

The flash


enum {
   UIImagePickerControllerCameraFlashModeOff  = -1,
   UIImagePickerControllerCameraFlashModeAuto = 0,
   UIImagePickerControllerCameraFlashModeOn   = 1
}; typedef NSInteger UIImagePickerControllerCameraFlashMode;

4. Two main delegate method examples

// A callback after the user selects the image
- (void)imagePickerController: (UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info 

    if (picker == picker_camera_)  
    { 
        // If it is Camera-Derived image So save it  
        UIImage* original_image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
    } // info dictionary keys
UIKIT_EXTERN NSString *const UIImagePickerControllerMediaType;      // an NSString (UTI, i.e. kUTTypeImage)
UIKIT_EXTERN NSString *const UIImagePickerControllerOriginalImage;  // a UIImage
UIKIT_EXTERN NSString *const UIImagePickerControllerEditedImage;    // a UIImage
UIKIT_EXTERN NSString *const UIImagePickerControllerCropRect;       // an NSValue (CGRect)
UIKIT_EXTERN NSString *const UIImagePickerControllerMediaURL;       // an NSURL
UIKIT_EXTERN NSString *const UIImagePickerControllerReferenceURL    NS_AVAILABLE_IOS(4_1);  // an NSURL that references an asset in the AssetsLibrary framework
UIKIT_EXTERN NSString *const UIImagePickerControllerMediaMetadata   NS_AVAILABLE_IOS(4_1);  // an NSDictionary containing metadata from a captured photo
    // Get the edited image  
    UIImage* image = [info objectForKey: @"UIImagePickerControllerEditedImage"];      // Convert the picture to NSData Type of data to save the file ( Put it in a sandbox ) 
    NSData *imageData;      // Determine if the picture is png Formatted file  
    if (UIImagePNGRepresentation(image)) {          // Return to png The image.  
        imageData = UIImagePNGRepresentation(image);     }else {         // Return to JPEG image
        imageData = UIImageJPEGRepresentation(image, 1.0);      }     // Path joining together , write -----
    NSString * imageSavePath = [[[HMTMySqliteDataHandle shareInstance]saveImagesPath] stringByAppendingPathComponent:@" The custom . The custom "];     [imageData writeToFile:imageSavePath atomically:YES];      // Close the album interface   
    [self dismissModalViewControllerAnimated:YES]; 
    [picker release]; 
} // User selects cancel  
- (void) imagePickerControllerDidCancel: (UIImagePickerController *)picker 

    // Close the album interface
    [self dismissModalViewControllerAnimated:YES]; 
    [picker release]; 
}  


Related articles: