-3

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.

Ryan M
  • 269
  • 2
  • 11
user1735714
  • 31
  • 1
  • 9

3 Answers3

3

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

http://www.techotopia.com/index.php/An_Example_iOS_4_iPad_Camera_and_UIImagePickerController_Application_(Xcode_4)

Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
lukaswelte
  • 2,951
  • 1
  • 23
  • 45
1

<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];
}
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
SpaceDust__
  • 4,844
  • 4
  • 43
  • 82
0

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

}
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • Just to add to that, you need to implement the `UIImagePickerControllerDelegate` which includes declaring that in the .h file. http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImagePickerControllerDelegate_Protocol/UIImagePickerControllerDelegate/UIImagePickerControllerDelegate.html – Sam Clewlow Jan 22 '13 at 19:52
  • hi i get, this error IStatusBarStyleBlackTranslucent is not available on this device. 2013-01-22 21:37:27.745 Grapher2[52960:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController' – user1735714 Jan 22 '13 at 20:45
  • this is the line that crashes [self presentModalViewController:imagePicker animated:YES]; – user1735714 Jan 22 '13 at 20:52
  • You didn't specify you're on iPad. This code works on iPhone. You should look at the documentation for UIPopoverController, or post a separate question on that topic. – Aaron Brager Jan 22 '13 at 21:05
  • thanks i did know it is different. – user1735714 Jan 22 '13 at 21:21