Graphics Programs Reference
In-Depth Information
In addition to a source type, the
UIImagePickerController
instance needs a deleg-
ate to handle requests from its view. When the user taps the
Use Photo
button on the
UIImagePickerController
's interface, the delegate is sent the message
im-
agePickerController:didFinishPickingMediaWithInfo:
. (The delegate
receives another message -
imagePickerControllerDidCancel:
- if the process
was cancelled.)
In
DetailViewController.m
, add the following code to
takePicture:
. (Re-
member - there's already a stub for this method, so locate the stub in
DetailViewCon-
troller.m
and add the following code there.)
- (IBAction)takePicture:(id)sender
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
// If our device has a camera, we want to take a picture, otherwise, we
// just pick from photo library
if ([UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
} else {
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
// This line of code will generate a warning right now, ignore it
[imagePicker setDelegate:self];
}
Once the
UIImagePickerController
has a source type and a delegate, it's time to
put its
view
on the screen. Unlike other
UIViewController
subclasses you've used,
an instance of
UIImagePickerController
is presented
modally
. When a view con-
troller is
modal
, it takes over the entire screen until it has finished its work.
To present a view modally,
presentViewController:animated:completion:
is sent to the
UIViewController
whose
view
is on the screen. The view controller to
be presented is passed to it, and its view slides up from the bottom of the screen.
In
DetailViewController.m
, add code to the end of
takePicture:
to present
the
UIImagePickerController
.
[imagePicker setDelegate:self];
// Place image picker on the screen
[self presentViewController:imagePicker animated:YES completion:nil];
}