I need some help. I need to integrate the camera into my app, and I want to learn about the following:
- I need the camera button on my view so that clicking it opens the camera view.
- I take a picture
- I need to code so that I have access to the Phone Gallery and then
display a pic in another view.
Could anyone please point me in the right direction?
Well, UIImagePickerController
is the tool you need. It will do most of the stuff in that checklist.
For the button you can create a custom button with graphics or if you are planning to use a tool bar or a navigation bar to hold your buttons, you can create the bar button using the UIBarButtonSystemItemCamera
system item. This will give you the framework image.
On tapping it, you will create a UIImagePickerController
instance and present it modally.
UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
[self presentModalViewController:imagePicker animated:YES];
[picker release];
As you must've noticed that it has a delegate
property which is defined as id < UIImagePickerControllerDelegate, UINavigationControllerDelegate> delegate;
so you will have to adopt both the protocols but in most cases you implement only two methods – imagePickerControllerDidCancel:
and imagePickerController:didFinishPickingMediaWithInfo:
. There is another method in UIImagePickerControllerDelegate
protocol but that's deprecated. Don't use it even if you see it mentioned a lot around here. You would expect the cancel handler to be written like this,
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissModalViewControllerAnimated:YES];
}
The other methods is where you do most of the stuff.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
// You have the image. You can use this to present the image in the next view like you require in `#3`.
[self dismissModalViewControllerAnimated:YES];
}
Taking the picture is done automatically by the UIImagePickerController
instance. However if you want to override their controls, you can do so by setting showsCameraControls
to NO
and then implementing your own cameraOverlayView
. If you've done so and have assigned a button to take the picture, you can actually trigger the picture action using the takePicture
method. So this should address #2
.
You can use other properties to adjust your image picker too. For example, you can limit the user to only taking images using the mediaTypes
property.
Paraphrasing the docs, dismissModalViewControllerAnimated:
is deprecated from iOS6 onwards. Use dismissViewControllerAnimated:completion:
instead.