views:

3009

answers:

2

I followed a great tutorial (http://iphone.zcentric.com/2008/08/28/using-a-uiimagepickercontroller/) on using a UIImagePickerController to get images from the Photo Album or Camera on the iPhone. The problem is, the tutorial is a bit dated and documentation cites a method used by the delegate as being depreciated since 3.0. The problem is, the documentation failed to provide clues as to what to use in place? The deprecated method is:

– imagePickerController:didFinishPickingImage:editingInfo:

The above method is used as follows:

- (void) imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)img editingInfo:(NSDictionary*)editInfo
{

    image.image = img;
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];


}

QUESTION: What is currently used in place of the deprecated method?

+6  A: 

Look at the apple docs..

imagePickerController:didFinishPickingImage:editingInfo:

Tells the delegate that the user picked an image. This method is optional. (Deprecated in iPhone OS 3.0. Use imagePickerController:didFinishPickingMediaWithInfo: instead.)

Mk12
+11  A: 

Here's how to use the new image picker API in a nutshell.

First, you need a class declared like this since it's setting itself as the image picker delegate:

@interface MyClass : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
UIImagePickerController*    imagePicker;
}
@property(nonatomic,retain) UIImagePickerController* imagePicker;

- (IBAction) takePicture:(id)sender;

@end

The method that brings up the image picker would go something like this. It's declared here as an IBAction so you can directly wire it to a control (like a button) in Interface Builder. It also checks so that if you're on an iPhone it brings up the camera interface but on an iPod Touch it brings up the gallery picker:

#import <MobileCoreServices/UTCoreTypes.h>
...
@synthesize imagePicker = _imagePicker;
...

- (void) takePicture:(id)sender
{
     if (!_imagePicker) {
      self.imagePicker = [[UIImagePickerController alloc] init];
      self.imagePicker.delegate = self;
     }

     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
      self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
      NSArray* mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
      self.imagePicker.mediaTypes = mediaTypes;
     } else {
      self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
      self.imagePicker.allowsImageEditing = YES; 
     }

    [self presentModalViewController:self.imagePicker animated:YES];
}

Then you need these two methods:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [[picker parentViewController] dismissModalViewControllerAnimated:YES]; 

    // MediaType can be kUTTypeImage or kUTTypeMovie. If it's a movie then you
    // can get the URL to the actual file itself. This example only looks for images.
    //   
    NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    // NSString* videoUrl = [info objectForKey:UIImagePickerControllerMediaURL];

    // Try getting the edited image first. If it doesn't exist then you get the
    // original image.
    //
    if (CFStringCompare((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {  
     UIImage* picture = [info objectForKey:UIImagePickerControllerEditedImage];
     if (!picture)
      picture = [info objectForKey:UIImagePickerControllerOriginalImage];  

            // **You can now do something with the picture.
    }
    self.imagePicker = nil;
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{ 
    [[picker parentViewController] dismissModalViewControllerAnimated:YES]; 
    self.imagePicker = nil;
}
Ramin
You deserved accepted answer.
Mk12
Heh, thanks. I don't hang out here for the points. It's for the good karma and you just bumped it up a notch :-)
Ramin
Isn't allowsImageEditing deprecated too?
Jane Sales
I believe it's still available in OS 3.0. Maybe you're thinking of an as yet unreleased OS ;-)
Ramin