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:
- This code
allocs
aUIImagePickerController
, but neverreleases
it. Therelease
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. - The
delegate
of aUIImagePickerController
must actually adopt theUINavigationControllerDelegate
protocol as well as theUIImagePickerControllerDelegate
protocol. I don’t know much about theUINavigationControllerDelegate
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.