Search code examples
iphoneobjective-cuiimagepickercontroller

What function should I implement to return a selected picture from gallery? iphone sdk


What function should I implement to return a selected picture from gallery?


Solution

  • Use UIImagePickerController. In this example, I'm using the built-in image cropper as well.

    - (void) showImage {
        UIImagePickerController *imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;
        imagePickerController.mediaTypes = [NSArray arrayWithObject: (NSString *) kUTTypeImage];
        [self presentModalViewController: imagePickerController animated: YES];
    }
    
    - (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info {
        UIImage *image = [info objectForKey: UIImagePickerControllerEditedImage];
        self.editedImage = image;
        [self dismissModalViewControllerAnimated: YES];
    }
    
    - (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker {
        [self dismissModalViewControllerAnimated: YES];
    }
    

    This will allow the user to pick an image. If you don't want the cropping, set imagePickerController.allowsEditing = NO and use the key UIImagePickerControllerOriginalImage to get the resulting image back.