SoFunction
Updated on 2025-04-11

Summary of UIImagePickerController Image Picker in iOS

UIImagePickerController is used to manage customizable, system-supported user interfaces for obtaining pictures and videos on the device. It can also be used to select stored pictures and videos in the App. A UIImagePickerController manages user interactions and passes these interaction results to a proxy object. This class cannot be inherited and modified except for custom cameraOverlayView.

First, briefly explain some properties of UIImagePickerController, and then add the code.

1. Common properties

(1) The type of selection interface displayed by the sourceType controller,

Contains three enum values

Copy the codeThe code is as follows:

enum {
UIImagePickerControllerSourceTypePhotoLibrary, //Select pictures or videos in the picture library
UIImagePickerControllerSourceTypeCamera, //Used to take photos or videos
UIImagePickerControllerSourceTypeSavedPhotosAlbum   //Select pictures or videos in the album
};
typedef NSUInteger UIImagePickerControllerSourceType;

(2)mediaTypes

The default value is kUTTypeImage, which means that users can only choose static images or take static images (relative to videos)

When mediaTypes is set to kUTTypeImage and kUTTypeMovie (if the device supports it), you can choose to operate the video while operating the picture.

When using KUTTypeImage, KUTTypeMovie, you need to import #import <MobileCoreServices/>

(3)allowEditing

Whether users can modify pictures or videos, the default is NO

(3)cameraOverlayView

Displays at the front of the default image selection interface.

2. Basic usage

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

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

(3) If you need to adjust mediaTypes (default is kUTTypeImage), adjust

(4) Display interface, pop up using modal.

(5) When the user operation is completed (cancel or select a certain picture and a certain video), the agent will trigger the method, then close the interface and perform related processing.

3. Detailed explanation of the other two sourceTypes

The other two sourceTypes are used in a relatively single way. Let’s explain in detail the situation when SourceType is UIImagePickerControllerSourceTypeCamera.

Common properties:

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

Used to shoot videos and select videos. If you select an existing video and the video quality is higher than the videoQuality set, the video will be converted to low quality.

(What if there is video hell video quality)

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

(3)cameraViewTransform

‍These attributes are only available in camera

(4)cameraDevice

Copy the codeThe code is as follows:

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

(5)cameraCaptureMode

The mode selected when opening the camera interface

Copy the codeThe code is as follows:

enum {
UIImagePickerControllerCameraCaptureModePhoto, //Use photography mode by default
UIImagePickerControllerCameraCaptureModeVideo   //Default camera mode
};
typedef NSUInteger UIImagePickerControllerCameraCaptureMode;

(6)cameraFlashMode

flash

Copy the codeThe code is as follows:

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

typedef NSInteger UIImagePickerControllerCameraFlashMode;


IV. Two main delegate method examples
Copy the codeThe code is as follows:

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

    if (picker == picker_camera_)  
    { 
//If it is an image from the camera, save it first
        UIImage* original_image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
    }

// info dictionary keys
UIKIT_EXTERN NSString *const UIImagePickerControllerMediaType;      // an NSString (UTI, . 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


// Obtain edited pictures
    UIImage* image = [info objectForKey: @"UIImagePickerControllerEditedImage"]; 

// Convert the image to NSData type data to save the file (storage it in the sandbox)
    NSData *imageData; 

// Determine whether the picture is a file in png format
    if (UIImagePNGRepresentation(image)) { 

// Return to png image.
        imageData = UIImagePNGRepresentation(image);

    }else { 

// Return to JPEG image
        imageData = UIImageJPEGRepresentation(image, 1.0); 

    }

// Path stitching, writing -----
NSString * imageSavePath = [[[[HMTMySqliteDataHandle shareInstance]saveImagesPath] stringByAppendingPathComponent:@"Custom.Custom"];

    [imageData writeToFile:imageSavePath atomically:YES]; 

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

// User chooses to cancel
- (void) imagePickerControllerDidCancel: (UIImagePickerController *)picker 

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