Lesson 31

Using the Camera and Photo Library

All iOS 9 devices have at least one camera. When a user takes a picture with the camera, the image is stored in the device's photo library. This lesson shows you how to allow the user to pick an image from the photo library or take a new picture with the camera and use it in your application.

The UIKit framework contains a class called UIImagePickerController designed specifically to allow you to access the camera and photo library from your applications. This class presents its own user interface (see Figure 31.1) that allows a user to browse through the photo library or control the camera. All you have to do is present this view controller in your application and provide a delegate method whose methods are called when the user has finished selecting an image.

Two screenshots of iOS device, with Photos menu, with the word Cancel on the right top, and folders Moments and Camera Roll listed; and Camera Roll with 10 images below.

Figure 31.1

The image picker controller can also be used to record videos and access these recorded videos within your application.

Selecting the Image Source

The UIImagePickerController class can be used to access the contents of either the photo library, saved photos album, or the camera. You can specify the source by providing a value for the sourceType property. This value can be one of the following:

  • UIImagePickerControllerSourceType.PhotoLibrary
  • UIImagePickerControllerSourceType.Camera
  • UIImagePickerControllerSourceType.SavedPhotosAlbum

To check if a particular ...

Get Swift iOS 24-Hour Trainer 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.