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]; }