Capturing Stills and Video

The UIImagePickerViewController is an Apple-supplied interface for choosing images and movies, and taking new images or movies (on supported devices). This class handles all of the required interaction with the user and is very simple to use. All you need to do is tell it to start, then dismiss it after the user selects an image or movie.

Let’s go ahead and build a simple application to illustrate how to use the image picker controller. Open Xcode and start a new project. Select a View-based Application for the iPhone, and name it Media when requested.

The first thing to do is set up the main view. This is going to consist of a single button that is pressed to bring up the Image Picker controller. An UIImageView will display the image, or thumbnail of the video, that is captured.

Select the MediaViewController.h interface file to open it in the editor and add a UIButton and an associated method to the interface file. Flag these as an IBOutletand IBAction respectively. You also need to add a UIImageView to display that image returned by the image picker, which also needs to be flagged as an IBOutlet. Finally, add a UIImagePickerController, and flag the view controller as both UIImagePickerControllerDelegate and UINavigationControllerDelegate. The code to add to the default template is shown in bold:

#import <UIKit/UIKit.h>

@interface MediaViewController : UIViewController
      <UIImagePickerControllerDelegate, UINavigationControllerDelegate> {

 IBOutlet UIButton ...

Get Basic Sensors in iOS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.