Uiimagepickercontroller image selector-call the camera and photo library on the iPhone

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.