Have you ever found the need in your app to let the user take a Picture or choose one from the Camera Roll? Follow and find
how easy is to add this feature to your app.
The iOS SDK provides a native controller that handles the hard bits for you. This controller is UIImagePickerController.
In this particular example it will bring the camera. There are other options as well.
123456
// This will bring the CamerapickerController.sourceType=UIImagePickerControllerSourceTypeCamera;// Option for showing the photo librarypickerController.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;// This will just show the photos / videos viewpickerController.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
Note that the last two options require users to allow the app to access their photos / videos.
Don’t forget to check if the source type is available:
1234
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){// Set sourceType to UIImagePickerControllerSourceTypeCamera// and present controller}
If you do not care about movies, just still images we can set the media typpe to kUTTypeImage.
123
#import <MobileCoreServices/MobileCoreServices.h>// Don't forget to add MobileCoreServices.framework to the projectpickerController.mediaTypes=@[(NSString*)kUTTypeImage];
This class also supports the ability to let users crop and scale pictures. This is very useful if want to get square pics, for instance to update a social profile picture.
-(void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary*)info{[selfdismissViewControllerAnimated:YEScompletion:NULL];// UIImagePickerControllerEditedImage is useful if you allowed// editing of imagesUIImage*image=[infoobjectForKey:UIImagePickerControllerEditedImage];if(image==nil)image=[infoobjectForKey:UIImagePickerControllerOriginalImage];// Do something with the image}
As you can see, UIImagePickerController is quite easy to implement and it has lots of options that you can make use of.
It also allows you to set a custom overlay and define your own camera buttons.