15.2. Taking Photos with the Camera
Problem
You want to ask the user to take a photo with the camera on his iOS device, and you want to access that photo once the user is done.
Solution
Instantiate an object of type UIImagePickerController and present it as a
modal view controller on your current view controller. Here is our view
controller’s declaration:
#import "ViewController.h"#import <MobileCoreServices/MobileCoreServices.h>@interfaceViewController()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>@end@implementationViewController<#Restofyourcodegoeshere#>
The delegate of an instance of UIImagePickerController must conform to the
UINavigationControllerDelegate and
UIImagePickerControllerDelegate
protocols. If you forget to include them in your source file(s), you’ll
get warnings from the compiler when assigning a value to the delegate
property of your image picker controller. Please bear in mind that you
can still assign an object to the delegate property of an instance of
UIImagePickerController where that
object does not explicitly conform to the UIImagePickerControllerDelegate and UINavigationControllerDelegate protocols, but
implements the required methods in these protocols. I, however, suggest
that you give a hint to the compiler that the delegate object does, in
fact, conform to the aforementioned protocols in order to avoid getting
compiler warnings.
In our view controller, we will attempt to display an image picker controller as a modal view ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access