Taking Pictures

Today it’s just a quick note on using the iPhone’s camera to take pictures from within your app. They’ve made this really easy. Begin by reading the section in the iPhone Application Programming Guide that covers the UIImagePickerController class.

That documentation provides the following code snippet, which works pretty well (with two caveats):

-(BOOL)startCameraPickerFromViewController:(UIViewController*)controller usingDelegate:(id<UIImagePickerControllerDelegate>)delegateObject
    if ( (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
            || (delegateObject == nil) || (controller == nil))
        return NO;
    UIImagePickerController* picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.delegate = delegateObject;
    picker.allowsImageEditing = YES;
    // Picker is displayed asynchronously.
    [controller presentModalViewController:picker animated:YES];
    return YES;

Two things to note:

  1. This code allocs a UIImagePickerController, but never releases it. The release is assumed to happen in the same delegate code that dismisses the modal view. This seems an odd arrangement to me, and I think you could (auto)release the controller in this code just as well.
  2. The delegate of a UIImagePickerController must actually adopt the UINavigationControllerDelegate protocol as well as the UIImagePickerControllerDelegate protocol. I don’t know much about the UINavigationControllerDelegate protocol, but its methods seem to be optional; at any rate, sticking it into the list of protocols adopted by the delegate, with no other changes to the code, seemed to work ok.
Share and Enjoy:
  • Twitter
  • Facebook
  • Digg
  • Reddit
  • HackerNews
  • del.icio.us
  • Google Bookmarks
  • Slashdot
This entry was posted in iPhone. Bookmark the permalink.

Comments are closed.