1

I currently have a UIImageView and a UIImage. How do I allow the user to select one of their photos from the camera roll?

There are possible duplicates, but I've had no luck. This might possibly be because I don't hook things up in the Interface Builder.

I would like something simple like UIImage * image = [UIImage imageFromCameraRoll], but unfortunately, that's not possible (I don't think so, at least).

Community
  • 1
  • 1
Scott
  • 2,568
  • 1
  • 27
  • 39
  • 2
    The class reference for `UIImagePickerViewController` is your best friend. –  Jan 14 '13 at 22:25

2 Answers2

3

Try this code

It is help to you get image from cameraroll & photolibrary. using UIImagePickerViewController

if you take Picture

  1. From camera (in both open As UINavigationcontroller).
  2. From Gallery(for ipad open as UIpopovercontroller & for iphone open as nvigationcontroller).

First set delegate

@interface camera : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate,UIPopoverControllerDelegate>

get image from Camera

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController *imagePicker =
            [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            imagePicker.sourceType =
            UIImagePickerControllerSourceTypeCamera;
            imagePicker.mediaTypes = [NSArray arrayWithObjects:
                                      (NSString *) kUTTypeImage,
                                      nil];

            imagePicker.allowsEditing = YES;

            imagePicker.wantsFullScreenLayout = YES;

            [self presentViewController:imagePicker animated:YES completion:nil];
            newMedia = YES;
            iscamera = 0;
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error to access Camera"
                                                            message:@""
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];

        }

Get image from Gallery

 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        UIImagePickerController *_picker=nil;
        if (popoverController) {
            [popoverController dismissPopoverAnimated:NO];

        }
        _picker = [[UIImagePickerController alloc] init];
        _picker.delegate = self;        
        _picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        _picker.wantsFullScreenLayout = YES;

        //[popoverController presentPopoverFromBarButtonItem:sender
                               //   permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

           [self presentViewController:_picker animated:YES completion:nil];


        } else
        {
            popoverController = [[UIPopoverController alloc] initWithContentViewController:_picker];
            [popoverController setDelegate:self];
            [popoverController presentPopoverFromRect:btn.frame
                                               inView:self.view
                             permittedArrowDirections:UIPopoverArrowDirectionLeft
                                             animated:YES];
        }
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error access photo library"
                                                        message:@"your device non support photo library"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];

    }

Both Result you get in Delegate Method

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   // write your code here ........
 }
Dixit Patel
  • 3,190
  • 1
  • 24
  • 36
1

Use UIImagePickerController, see the Apple docs.

Select the source type:

@property (nonatomic) UIImagePickerControllerSourceType sourceType

From on on these:

UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
zaph
  • 111,848
  • 21
  • 189
  • 228