I am new to Iphone development and got a situation where I need to have a ViewController named PhotoSelectorViewController which will ask for one to pick images from phone library. I know UIImagePickerController is there but I don’t know how to use it correctly.
I tried following:
First Method:
- I created a ViewController named PhotoSelectorViewController with xib file.
- I remove View from xib file and added UIImagePickerController to the list.
- I don’t know what to do next to start the picker when one will create the instance of this class calling some init function in this ViewController.
Second Method:
- I created new project and added a view controller named PhotoSelectorViewController where there is a Window and a UIView only.
- On UIView viewDidLoad function I am creating instance of UIImagePickerController and adding it to the view.
- I don’t know how this picker will be started when one will create instance of this view controller?
Please help if you can.
The easiest way is to present it modally from the calling View Controller.
So, for instance, if you have a button on
MyControllercalledshowPhotos:Then, you need to implement
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)infoin this same controller as it (in my example) was set to be the delegate.
allowsEditingallows the user to pan, zoom and crop the image before use. Just set to NO if you don’t wish to allow that.Inside your delegate method, the dictionary contains the resulting
UIImage. You can obtain viaUIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];or
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];if you allow editing.