UIImagePickerController, Sandbox
UIImagePickerController1. + (BOOL) isSourceTypeAvailable :( UIImagePickerControllerSourceType) sourceType; check whether the specified source is available on the device.
// Check whether the photo source is available
[UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary] 2. allowsEditing default NO
Allow editing?
Allow editing.
[ImagePicker setAllowsEditing: YES];
3. videoMaximumDuration
Sets the maximum video duration of UIImagePicker. The default value is 10 minutes.
4. + availableMediaTypesForSourceType: // specifies the media type available for the source.
// Obtain the supported media types in Camera mode
NSArray * availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
5. sourceType
Set the UIImagePicker photo source type. The default value is 3.
Photo Source Type
UIImagePickerControllerSourceTypeCamera camera
UIImagePickerControllerSourceTypePhotoLibrary)
UIImagePickerControllerSourceTypeSavedPhotosAlbum)
6. Steps for using UIImagePicker:
Check whether the specified source is available. isSourceTypeAvailable: method.
Check the available media (video or image only) availableMediaTypesForSourceType: method.
Set media properties on the page.
Display Interface: Use presentViewController: animated: completion: method. popover form in iPad. Make sure sourceType is valid.
Related Operations: remove the view.
If you want to create an image picker with a fully customized interface to browse images, use the class in the Assets Library Framework Reference. ("Media Capture and Access to Camera" in the AV Foundation Programming Guide ")
7. Set the source
+ AvailableMediaTypesForSourceType: // specifies the media type available for the source.
+ IsSourceTypeAvailable: // specifies whether the source is available on the device.
SourceType
// The source type must be specified before running the relevant interface. It must be valid; otherwise, an exception is thrown. The value is changed when the picker is displayed, and the picker changes accordingly. The default value is UIImagePickerControllerSourceTypePhotoLibrary.
8. Set the picker attribute
AllowsEditing // editable
Delegate
MediaTypes
// Indicates the media type displayed in picker. apply availableMediaTypesForSourceType before setting each type: Check. if it is null or the array type is unavailable, an exception occurs. the default value is kUTTypeImage. Only images can be displayed.
9. video selection parameters
VideoQuality // specifies the encoding quality when a video is taken. This parameter is valid only when mediaTypes contains the kUTTypeMovie.
VideoMaximumDuration // second. The maximum video recording time. The default value is 10 minutes. This parameter is only valid when mediaTypes contains kUTTypeMovie.
10. Custom Interface
ShowsCameraControls
// Indicates whether picker displays the default camera controls. the default value is YES. Set it to NO to hide the default controls to use the custom overlay view. (In this way, you can select multiple options instead of selecting a picker ). only the UIImagePickerControllerSourceTypeCamera source is valid. Otherwise, NSInvalidArgumentException is abnormal.
CameraOverlayView
// Custom view displayed on picker. Valid only when the source is UIImagePickerControllerSourceTypeCamera. Otherwise, an NSInvalidArgumentException exception is thrown.
CameraViewTransform
// Pre-animation. Only the pre-image is affected, and the custom overlay view and default picker are invalid. It is only valid when the picker source is UIImagePickerControllerSourceTypeCamera; otherwise, NSInvalidArgumentException is abnormal.
11. Select Media
-TakePicture
// Use the camera to select an image. You can select multiple custom overlay instances. If an image is being selected, the call is invalid. You must wait until delegate receives the imagePickerController: didFinishPickingMediaWithInfo: message before selecting the image again. Non-UIImagePickerControllerSourceTypeCamera source will cause an exception.
-StartVideoCapture
-StopVideoCapture
// After the video is selected, the system calls delegate's imagePickerController: didFinishPickingMediaWithInfo: method.
12. Set the camera
CameraDevice // lens used (default rear)
+ IsCameraDeviceAvailable: // whether the camera device is available.
+ AvailableCaptureModesForCameraDevice: // available device selection mode
CameraCaptureMode // camera capture mode
CameraFlashMode // specifies the flashlight mode (automatic by default)
+ IsFlashAvailableForCameraDevice: // whether the flash capability exists
13. UIImagePickerControllerDelegate
Use UIImageWriteToSavedPhotosAlbum to save the image. Use UISaveVideoAtPathToSavedPhotosAlbum to save the video. 4.0 and use writeImageToSavedPhotosAlbum: metadata: completionBlock: to save the metadata.
-(Void) imagePickerController :( UIImagePickerController *) picker didFinishPickingMediaWithInfo :( NSDictionary *) info
// Contains the selected image or video URL. For details, see "Editing Information Keys ."
// If the Editable attribute is set, the picker will pre-display the selected media, and the edited and initial media will be saved in info.
-ImagePickerControllerDidCancel:
-ImagePickerController: didFinishPickingImage: editingInfo: // Deprecated in iOS 3.0
NSString * const UIImagePickerControllerMediaType; // media type
NSString * const UIImagePickerControllerOriginalImage; // original unedited Image
NSString * const UIImagePickerControllerEditedImage; // The edited image.
NSString * const UIImagePickerControllerCropRect; // editable source image (valid ?) Region
NSString * const UIImagePickerControllerMediaURL; // The video path.
NSString * const UIImagePickerControllerReferenceURL; // the URL of the original selected item
NSString * const UIImagePickerControllerMediaMetadata; // this parameter is valid only when the camera is used and the image type is used. It contains the dictionary type for selecting image information.
14. Example of UIImagePickerController
The proxy of UIImagePickerController must comply with these two protocols. <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
# Pragma mark select a photo
-(Void) selectPhoto
{
// 1. first determine whether the photo source is available
If ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]) {
// 0) instantiate the Controller
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
// 1) set the photo source
[Picker setSourceType: UIImagePickerControllerSourceTypePhotoLibrary];
// 2) allow modification
[Picker setAllowsEditing: YES];
// 3) set proxy
[Picker setDelegate: self];
// 4) display controller
[Self presentViewController: picker animated: YES completion: nil];
} Else {
NSLog (@ "photo source unavailable ");
}
}
# Pragma mark-imagePicker proxy method
-(Void) imagePickerController :( UIImagePickerController *) picker didFinishPickingMediaWithInfo :( NSDictionary *) info
{
UIImage * image = info [@ "UIImagePickerControllerEditedImage"];
[_ ImageButton setImage: image forState: UIControlStateNormal];
// Disable the photo Selector
[Self dismissViewControllerAnimated: YES completion: nil];
// Save the photo to the application sandbox. Because data storage is involved, it is not related to the interface.
// You can use multiple threads to save images.
Dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
// Save the image
// 1. Obtain the image path
NSArray * docs = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * imagePath = [docs [0] stringByAppendingPathComponent: @ "abc.png"];
// 2. Convert to NSData for saving
NSData * imageData = UIImagePNGRepresentation (image );
[ImageData writeToFile: imagePath atomically: YES];
});
}