The Camera and Photo Library - Beginning iPhone Development: Exploring the iOS SDK, Seventh Edition (2014)

Beginning iPhone Development: Exploring the iOS SDK, Seventh Edition (2014)

Chapter 21. The Camera and Photo Library

By now, it should come as no surprise to you that the iPhone, iPad, and iPod touch have a built-in camera and a nifty application called Photos to help you manage all those awesome pictures and videos you’ve taken. What you may not know is that your programs can use the built-in camera to take pictures. Your applications can also allow the user to select from among and view the media already stored on the device. We’ll look at both of these abilities in this chapter.

Using the Image Picker and UIImagePickerController

Because of the way iOS applications are sandboxed, applications ordinarily can’t get access to photographs or other data that live outside their own sandboxes. Fortunately, both the camera and the media library are made available to your application by way of an image picker.

Using the Image Picker Controller

As the name implies, an image picker is a mechanism that lets you select an image from a specified source. When this class first appeared in iOS, it was used only for images. Nowadays, you can use it to capture video as well.

Typically, an image picker will use a list of images and/or videos as its source (see the left side of Figure 21-1). You can, however, specify that the picker use the camera as its source (see the right side of Figure 21-1).

image

Figure 21-1. An image picker in action. Users are presented with a list of images (left). Once an image is selected, it can be moved and scaled (right). And, yeah, sometimes my camera roll is just pictures of Clumsy Ninja. I blame my children for this

The image picker interface is implemented by way of a controller class called UIImagePickerController. You create an instance of this class, specify a delegate (as if you didn’t see that coming), specify its image source and whether you want the user to pick an image or a video, and then present it. The image picker will take control of the device to let the user select a picture or video from the existing media library. Or, the user can take a new picture or video with the camera. Once the user makes a selection, you can give the user an opportunity to do some basic editing, such as scaling or cropping an image, or trimming away a bit of a video clip. All of that behavior is implemented by the UIImagePickerController, so you really don’t need to do much heavy lifting here.

Assuming the user doesn’t press Cancel, the image or video that the user either captures or selects from the library will be delivered to your delegate. Regardless of whether the user selects a media file or cancels, your delegate is responsible for dismissing theUIImagePickerController so that the user can return to your application.

Creating a UIImagePickerController is extremely straightforward. You just create an instance the way you would with most classes. There is one catch, however: not every iOS device has a camera. Older iPod touches were the first examples of this, and the first-generation iPad is the latest. However, more such devices may roll off Apple’s assembly lines in the future. Before you create an instance of UIImagePickerController, you need to check to see whether the device your app is currently running on supports the image source you want to use. For example, before letting the user take a picture with the camera, you should make sure the program is running on a device that has a camera. You can check that by using a class method on UIImagePickerController, like this:

if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera]) {

In this example, we’re passing UIImagePickerControllerSourceTypeCamera to indicate that we want to let the user take a picture or shoot a video using the built-in camera. The method isSourceTypeAvailable: returns YES if the specified source is currently available. We can specify two other values in addition to UIImagePickerControllerSourceTypeCamera:

· UIImagePickerControllerSourceTypePhotoLibrary specifies that the user should pick an image or video from the existing media library. That image will be returned to your delegate.

· UIImagePickerControllerSourceTypeSavedPhotosAlbum specifies that the user will select the image from the library of existing photographs, but that the selection will be limited to the camera roll. This option will run on a device without a camera, where it is less useful but still allows you to select any screenshots you have taken.

After making sure that the device your program is running on supports the image source you want to use, launching the image picker is relatively easy:

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
[self presentViewController:picker animated:YES completion:nil];

Tip On a device that has more than one camera, you can select which one to use by setting the cameraDevice property to UIImagePickerControllerCameraDeviceFront or UIImagePickerControllerCameraDeviceRear. To find out whether a front or rear camera is available, use the same constants with the isCameraDeviceAvailable: method.

After we have created and configured the UIImagePickerController, we use a method that our class inherited from UIView called presentViewController:animated:completion: to present the image picker to the user.

Implementing the Image Picker Controller Delegate

To find out when the user has finished using the image picker, you need to implement the UIImagePickerControllerDelegate protocol. This protocol defines two methods: imagePickerController:didFinishPickingMediaWithInfo: andimagePickerControllerDidCancel:.

The imagePickerController:didFinishPickingMediaWithInfo: method is called when the user has successfully captured a photo or video, or selected an item from the media library. The first argument is a pointer to the UIImagePickerController that you created earlier. The second argument is an NSDictionary instance that will contain the chosen photo or the URL of the chosen video, as well as optional editing information if you enabled editing in the image picker controller (and if the user actually did some editing). That dictionary will contain the original, unedited image stored under the key UIImagePickerControllerOriginalImage. Here’s an example of a delegate method that retrieves the original image:

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *selectedImage = info[UIImagePickerControllerEditedImage];
UIImage *originalImage = info[UIImagePickerControllerOriginalImage];

// do something with selectedImage and originalImage

[picker dismissViewControllerAnimated:YES completion:nil];
}

The editingInfo dictionary will also tell you which portion of the entire image was chosen during editing by way of an NSValue object stored under the key UIImagePickerControllerCropRect. You can convert this NSValue instance into a CGRect, like so:

NSValue *cropValue = info[UIImagePickerControllerCropRect];
CGRect cropRect = [cropValue CGRectValue];

After this conversion, cropRect will specify the portion of the original image that was selected during the editing process. If you do not need this information, you can just ignore it.

Caution If the image returned to your delegate comes from the camera, that image will not be stored in the photo library automatically. It is your application’s responsibility to save the image, if necessary.

The other delegate method, imagePickerControllerDidCancel:, is called if the user decides to cancel the process without capturing or selecting any media. When the image picker calls this delegate method, it’s just notifying you that the user is finished with the picker and didn’t choose anything.

Both of the methods in the UIImagePickerControllerDelegate protocol are marked as optional, but they really aren’t, and here is why: modal views like the image picker must be told to dismiss themselves. As a result, even if you don’t need to take any application-specific actions when the user cancels an image picker, you still need to dismiss the picker. At a bare minimum, your imagePickerControllerDidCancel: method will need to look like this for your program to function correctly:

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}

Road Testing the Camera and Library

In this chapter, we’re going to build an application that lets the user take a picture or shoot some video with the camera. Or, the user can select something from the photo library, and then display the selection on the screen (see Figure 21-2). If the user is on a device without a camera, we will hide the New Photo or Video button and allow selection only from the photo library.

image

Figure 21-2. The Camera application in action

Designing the Interface

Create a new project in Xcode using the Single View Application template, naming the application Camera. The first order of business is to add a couple of outlets to this application’s view controller. We need one to point to the image view so that we can update it with the image returned from the image picker. We’ll also need an outlet to point to the New Photo or Video button so that we can hide the button if the device doesn’t have a camera.

We also need two action methods: one for the New Photo or Video button and one that lets the user select an existing picture from the photo library.

Expand the Camera folder so that you can get to all the relevant files. Select ViewController.m and add the following protocol conformance declarations and properties to the class extension:

#import "ViewController.h"

@interface ViewController ()
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *takePictureButton;

@end

The first thing you might notice is that we’ve actually conformed our class to two different protocols: UIImagePickerControllerDelegate and UINavigationControllerDelegate. Because UIImagePickerController is a subclass ofUINavigationController, we must conform our class to both of these protocols. The methods in UINavigationControllerDelegate are optional, and we don’t need either of them to use the image picker; however, we do need to conform to the protocol, or the compiler will give us a warning later on.

The other thing you might notice is that, while we’ll be dealing with an instance of UIImageView for displaying a chosen image, we don’t have anything similar for displaying a chosen video. UIKit doesn’t include any publicly available class like UIImageView that works for showing video content, so we’ll have to show video using another technique instead. When we get to that point, we will use an instance of MPMoviePlayerController, grabbing its view property and inserting it into our view hierarchy. This is a highly unusual way of using any view controller, but it’s actually an Apple-approved technique to show video inside a view hierarchy.

We’re also going to add two action methods that we want to connect our buttons to. For now, we’ll just create empty implementations so that Interface Builder can see them. We’ll fill in the actual code later:

- (IBAction)shootPictureOrVideo:(UIButton *)sender {
}

- (IBAction)selectExistingPictureOrVideo:( UIButton *)sender {
}

Save your changes and select Main.storyboard to edit the GUI in Interface Builder.

The layout we’re going to build for this application is very simple—just an image view and two buttons. The finished layout is shown in Figure 21-3. Use this as a guide as you work.

image

Figure 21-3. The storyboard layout for the Camera application

Drag two Buttons from the library to the window labeled View. Place them one above the other, aligning the bottom button with the bottom blue guideline. Double-click the top button and give it a title of New Photo or Video. Now double-click the bottom button and give it a title of Pick from Library. Next, drag an Image View from the library and place it above the buttons. Expand the image view to take up the entire space of the view above the buttons, as shown earlier in Figure 21-2. In the Attributes Inspector, change the image view’s background to black and set itsMode to Aspect Fit, which will cause it to resize images so that they fit within its bounds, but maintain their original aspect ratio.

Now Control-drag from the View Controller icon to the image view and select the imageView outlet. Drag again from View Controller to the New Photo or Video button and select the takePictureButton outlet.

Next, select the New Photo or Video button and bring up the connections inspector. Drag from the Touch Up Inside event to View Controller and select the shootPictureOrVideo: action. Now click the Pick from Library button, drag from the Touch Up Inside event in the connections inspector to View Controller, and select the selectExistingPictureOrVideo: action.

The final step, as usual, is to add auto layout constraints. Start by expanding the view controller in the Document Outline, and rename its view to Main View, and then add constraints as follows:

1. In the Document Outline, Control-drag from the Pick from Library button to the Main View, and then release the mouse. When the pop-up appears, hold down Shift and select Leading Space to Container Margin, Trailing Space to Container Margin, and Bottom Space to Bottom Layout Guide.

2. Control-drag from the New Photo or Video button to the Pick from Library button, release the mouse, and select Vertical Spacing.

3. Control-drag from the New Photo or Video button to Main View, release the mouse, hold down Shift and select Leading Space to Container Margin and Trailing Space to Container Margin.

4. Control-drag from New Photo or Video to the image view and select Vertical Spacing.

5. Control-drag from the image view to the Main View and use the Shift key to select Leading Space to Container Margin, Trailing Space to Container Margin, and Top Space to Top Layout Guide.

6. Hold down the Shift key and click both the New Photo or Video button and the Pick from Library button to select them, and then click the Pin button below the storyboard editor. In the pop-up, check the Width check box and press Add 2 Constraints, as shown inFigure 21-4.

image

Figure 21-4. Finishing the auto layout constraints for the camera application

All of the layout constraints are now in place, so save your changes.

Implementing the Camera View Controller

Select ViewController.m, where we have some more changes to make. Since we’re going to allow users to optionally capture a video, we need a property for an MPMoviePlayerController instance. Two more properties keep track of the last selected image and video, along with a string to determine whether a video or image was the last thing chosen. We also need to import a few additional headers to make this all work. Add the bold lines shown here:

#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <MobileCoreServices/UTCoreTypes.h>

@interface ViewController ()
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *takePictureButton;
@property (strong, nonatomic) MPMoviePlayerController *moviePlayerController;
@property (strong, nonatomic) UIImage *image;
@property (strong, nonatomic) NSURL *movieURL;
@property (copy, nonatomic) NSString *lastChosenMediaType;

@end

Now let’s enhance the viewDidLoad method, hiding the New Photo or Video button if the device we’re running on does not have a camera. We also implement the viewDidAppear: method, having it call the updateDisplay method, which we’ll implement soon. First, make these changes:

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if (![UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera]) {
self.takePictureButton.hidden = YES;
}
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self updateDisplay];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

It’s important to understand the distinction between the viewDidLoad and viewDidAppear: methods. The former is called only when the view has just been loaded into memory. The latter is called every time the view is displayed, which happens both at launch and whenever we return to our controller after showing another full-screen view, such as the image picker.

Next up are three utility methods, the first of which is the updateDisplay method. It is called from the viewDidAppear: method, which is called both when the view is first created and again after the user picks an image or video and dismisses the image picker. Because of this dual usage, it needs to make a few checks to see what’s what and set up the GUI accordingly. Add this code toward the bottom of the file:

- (void)updateDisplay {
if ([self.lastChosenMediaType isEqual:(NSString *)kUTTypeImage]) {
self.imageView.image = self.image;
self.imageView.hidden = NO;
self.moviePlayerController.view.hidden = YES;
} else if ([self.lastChosenMediaType isEqual:(NSString *)kUTTypeMovie]) {
if (self.moviePlayerController == nil) {
self.moviePlayerController = [[MPMoviePlayerController alloc]
initWithContentURL:self.movieURL];
UIView *movieView = self.moviePlayerController.view;
movieView.frame = self.imageView.frame;
movieView.clipsToBounds = YES;
[self.view addSubview:movieView];
[self setMoviePlayerLayoutConstraints];
} else {
self.moviePlayerController.contentURL = self.movieURL;
}
self.imageView.hidden = YES;
self.moviePlayerController.view.hidden = NO;
[self.moviePlayerController play];
}
}

This method shows the correct view based on the type of media that the user selected—the image view for a photograph and the movie player for a movie. The image view is always present, but the movie player is created and added to the user interface only when the user picks a movie for the first time. When we add the movie player, we need to ensure that it occupies the same space as the image view and we need to add layout constraints that ensure that remains the case even if the device is rotated. Here’s the code that adds the layout constraints:

- (void)setMoviePlayerLayoutConstraints {
UIView *moviePlayerView = self.moviePlayerController.view;
UIView *takePictureButton = self.takePictureButton;
moviePlayerView.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *views =
NSDictionaryOfVariableBindings(moviePlayerView, takePictureButton);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
@"H:|[moviePlayerView]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
@"V:|[moviePlayerView]-0-[takePictureButton]" options:0 metrics:nil
views:views]];
}

The horizontal constraints tie the movie player to the left and right sides of the main view, and the vertical constraints link it to the top of the main view and the top of the New Photo or Video button.

The final utility method, pickMediaFromSource:, is the one that both of our action methods call. This method is pretty simple. It just creates and configures an image picker, using the passed-in sourceType to determine whether to bring up the camera or the media library. We do so by adding this code toward the bottom of the file:

- (void)pickMediaFromSource:(UIImagePickerControllerSourceType)sourceType {
NSArray *mediaTypes = [UIImagePickerController
availableMediaTypesForSourceType:sourceType];
if ([UIImagePickerController
isSourceTypeAvailable:sourceType] && [mediaTypes count] > 0) {
UIImagePickerController *picker =
[[UIImagePickerController alloc] init];
picker.mediaTypes = mediaTypes;
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = sourceType;
[self presentViewController:picker animated:YES completion:NULL];
} else {
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"Error accessing media"
message:@"Unsupported media source."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES
completion:nil];
}
}

Next, implement the following action methods that we declared in the header:

- (IBAction)shootPictureOrVideo:(id)sender {
[self pickMediaFromSource:UIImagePickerControllerSourceTypeCamera];
}

- (IBAction)selectExistingPictureOrVideo:(id)sender {
[self pickMediaFromSource:UIImagePickerControllerSourceTypePhotoLibrary];
}

Each of these simply calls out to the pickMediaFromSource: method, passing in a constant defined by UIImagePickerController to specify where the picture or video should come from.

Now it’s finally time to implement the delegate methods for the picker view:

#pragma mark - Image Picker Controller delegate methods

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.lastChosenMediaType = info[UIImagePickerControllerMediaType];
if ([self.lastChosenMediaType isEqual:(NSString *)kUTTypeImage]) {
self.image = info[UIImagePickerControllerEditedImage];
} else if ([self.lastChosenMediaType isEqual:(NSString *)kUTTypeMovie]) {
self.movieURL = info[UIImagePickerControllerMediaURL];
}
[picker dismissViewControllerAnimated:YES completion:NULL];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}

The first delegate method checks to see whether a picture or video was chosen, makes note of the selection, and then dismisses the modal image picker. If the image is larger than the available space on the screen, it will be resized by the image view when it’s displayed, because we set the image view’s content mode to Aspect Fit when we created it. The second delegate method is called when the user cancels the image picking process and just dismisses the image picker.

That’s all you need to do. Compile and run the app. If you’re running on the simulator, you won’t have the option to take a new picture, but will only be able to choose from the photo library—as if you had any photos in your simulator’s photo library! If you have the opportunity to run the application on a real device, go ahead and try it. You should be able to take a new picture or movie, and zoom in and out of the picture using the pinch gestures. The first time the app needs to access the user’s photos on iOS, the user will be asked to allow this access; this is a privacy feature that was added back in iOS 6 to make sure that apps aren’t sneakily grabbing photos without users’ consent.

After choosing or taking a photo, if you zoom in and pan around before hitting the Use Photo button, the cropped image will be the one returned to the application in the delegate method.

It’s a Snap!

Believe it or not, that’s all there is to letting your users take pictures with the camera so that the pictures can be used by your application. You can even let the user do a small amount of editing on that image if you so choose.

In the next chapter, we’re going to look at reaching a larger audience for your iOS applications by making them oh-so-easy to translate into other languages. Êtes-vous prêt? Tournez la page et allez directement. Allez, allez!