How can I create the image picker in code?
I use iOS 6.0 , with ARC.
I would like to able to select the picture and somehow get UIImage of the selected image.
How can I create the image picker in code?
I use iOS 6.0 , with ARC.
I would like to able to select the picture and somehow get UIImage of the selected image.
The basic instantiation goes like this
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setDelegate:self];
imagePicker.modalPresentationStyle = UIModalPresentationOverFullScreen;
[self presentViewController:imagePicker animated:YES completion:nil];
There are three ways to get an image
//Pick from Camera
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;//Make sure to enable permission for this in info.plist; see: https://stackoverflow.com/a/44690117/2057171
//Pick from all folders in the gallery
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//Pick from PhotosAlbum(camera roll)
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
Then check for the existence of the source
//Check Camera available or not
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
//Check PhotoLibrary available or not
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
//Check front Camera available or not
if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront])
You should implement the delegate protocol to get the image the user selected
//Tells the delegate that the user picked a still image or movie.
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[imagepicker dismissModalViewControllerAnimated:YES];
}
//Tells the delegate that the user cancelled the pick operation.
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
}
A simple Tutorial to use the UIImagePickerController
<UIImagePickerControllerDelegate>
add delegate to your @interface ViewController
line in your.h
file
make sure you have pics in your simulator if you are going to use simulator as debugger then call following methods
- (void)showImagePicker
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.modalPresentationStyle = UIModalPresentationOverFullScreen;
[self presentModalViewController:imagePicker animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Get the selected image.
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissModalViewControllerAnimated:YES];
}
To present it:
UIImagePickerController *picker;
picker = [[UIImagePickerController alloc] init];
[picker setDelegate:self];
[picker setSourceType:type];
[self presentViewController:picker animated:YES completion:nil];
To get the UIImage:
- (void)imagePickerController:(UIImagePickerController *)thePicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Do something with "image"
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// users typically expect that if they took a photo it will be saved
if (thePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
}