Uiimagepickercontroller the image selector is a navigation controller class that allows you to add simple image selection functions or camera interfaces to applications. The user will see an image selection screen where a photo is selected. The source of the photo is his own photo library, saved album or camera. After you select a photo, the delegate of the selector is notified through the method in the uiimagepickerdelegate protocol.
You can use the uiimagepickercontroller class to create an image selector and add it to a window as an independent navigation controller.
1. Create and add it to the view
Add the uinavigationcontrollerdelegate and uiimagepickercontrollerdelegate protocols to the. h file.
Uiimagepickercontrollersourcetype sourcetype = uiimagepickercontrollersourcetypecamera;
If (! [Uiimagepickercontroller issourcetypeavailable: uiimagepickercontrollersourcetypecamera])
{
Sourcetype = uiimagepickercontrollersourcetypephotolibrary;
}
Uiimagepickercontroller * picker = [[uiimagepickercontroller alloc] init];
Picker. Delegate = self;
Picker. allowsediting = yes;
Picker. sourcetype = sourcetype;
[Self presentmodalviewcontroller: picker animated: Yes];
[Picker release];
Ii. Image Source
You can use the sourcetype attribute to set multiple image sources and present them to users:
Picker. sourcetype = uiimagepickercontrollersourcetypecamera;
You can use the following sources:
Enum {
Uiimagepickercontrollersourcetypephotolibrary, // photo library
Uiimagepickercontrollersourcetypecamera, // camera
Uiimagepickercontrollersourcetypesavedphotosalbum // Save the photo
};
Iii. image editing
You can set the allowsimageediting attribute to yes to enable the image editing function:
Picker. allowsediting = yes;
Iv. Image Selection
After you select an image, the selector delegate will receive a notification through the didfinishpickingimage method. The proxy will get a uiimage object containing the image. If the editing function is enabled, it will also get an nsdictionary containing the editing attribute.
Set the delegate of the selector to assign a delegate to the selector:
Picker. Delegate = self;
The following method is implemented in your delegate class, so that when you select an image, the delegate class will be notified:
-(Void) imagepickercontroller :( uiimagepickercontroller *) picker didfinishpickingimage :( uiimage *) image editinginfo :( nsdictionary *) editinginfo {
[Picker dismissmodalviewcontrolleranimated: Yes];
Uiimage * image = [[info objectforkey: uiimagepickercontrollereditedimage] retain]; // obtain the image just taken
/* Add and process the selected Image Code */
}
The parameters of the method include a pointer pointing to the image selector controller reporting the current operation, so that you can process multiple selector in one delegate. The parameter also includes a pointer to the uiimage object and a dictionary object, which contains information about how the image is scaled and moved on the screen.
You may also want to be notified when you cancel image selection. To achieve this goal, you must implement the imagepickercontrollerdidcancel method in the proxy. It will be called when the selection is canceled, with the pointer to the canceled image selector as the parameter:
-(Void) imagepickercontrollerdidcancel :( uiimagepickercontroller
*) Picker {
/* Add code to process and cancel the selected image */
}
5. The effect is as follows: