How does the iphone use the picture Picker Uiimagepickercontroller

Source: Internet
Author: User

How to use the image picker with iphone

1. Define class variables:

uiimagepickercontroller* Picker_library_;

2. Realize Uiimagepickercontrollerdelegate this delegate

3. Display the picture picker in modal mode

Picker_library_ = [[Uiimagepickercontroller alloc] init]; Picker_library_.sourcetype = uiimagepickercontrollersourcetypephotolibrary; picker_library_.allowsediting = YES; Picker_camera_.allowsimageediting=yes; Picker_library_.delegate = self; [Self presentmodalviewcontroller:picker_library_ animated:yes];

Where sourcetype specifies the source of several images:

Uiimagepickercontrollersourcetypephotolibrary: Indicates that all photos are displayed

Uiimagepickercontrollersourcetypecamera: means picking photos from the camera

Uiimagepickercontrollersourcetypesavedphotosalbum: Indicates that only photos are selected from the album.

AllowEditing and allowsimageediting are set to Yes, which means that users are allowed to edit the picture, otherwise they are not allowed to edit.

4. When the user chooses a picture, it is possible to call two different functions, depending on the version. So, if you want to support both high and low versions of compatibility, then

There are two functions to handle.

3.x callback after the user selects the picture

-(void) Imagepickercontroller: (Uiimagepickercontroller *) picker

Didfinishpickingmediawithinfo: (nsdictionary *) info

2.x callback after the user selects the picture

-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker didfinishpickingimage: (UIImage *) Image Editinginfo :(nsdictionary *) editinginfo

The processing code for the two functions is as follows:

3.x callback after the user selects the picture-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: ( Nsdictionary *) Info {if (picker = = Picker_camera_) {//If it is an image from the camera, save uiimage* original_image = [Info objectforkey : @ "Uiimagepickercontrolleroriginalimage"]; Uiimagewritetosavedphotosalbum (Original_image, Self, @selector (image:didFinishSavingWithError:contextInfo:), nil) ; }//Get edited picture uiimage* image = [info objectforkey: @ "Uiimagepickercontrollereditedimage"]; [Self dismissmodalviewcontrolleranimated:yes]; [Picker release]; }

2.x callback after the user selects the image-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker didfinishpickingimage: (UIImage *) Image editinginfo: (nsdictionary *) editinginfo {nsmutabledictionary * dict= [nsmutabledictionary Dictionarywithdictionary:editinginfo]; [Dict setobject:image forkey:@ "Uiimagepickercontrollereditedimage"]; Call the 3.x handler function directly [self imagepickercontroller:picker didfinishpickingmediawithinfo:dict]; }

5. User Cancel Selection

User selected Cancel-(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) Picker {[Self Dismissmodalviewcontrolleranimated:yes]; [Picker release]; }

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: 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.