Chapter 13
Building a Full Screen Camera with Gesture-based Controls
iOS provides two ways for developers to access the built-in camera for taking photos. The simple approach is to use UIImagePickerViewController
, which I briefly covered in the Beginning iOS Programming book. This class is very handy and comes with a standard camera interface. Alternatively, you can control the built-in cameras and capture images using the AVFoundation framework. Compared to UIImagePickerViewController
, AVFoundation framework is more complicated, but also far more flexible and powerful for building a fully custom camera interface.
In this chapter, we will see how to use the AVFoundation framework for capturing still images. You will learn a lot of stuff including:
- How to create a camera interface using the AVFoundation framework
- How to capture a still image using both the front-facing and back-facing camera
- How to use gesture recognizers to detect a swipe gesture
- How to provide a zoom feature for the camera app
- How to save an image to the camera roll
The core of AV Foundation media capture is an AVCaptureSession
object. It controls the data flow between an input (e.g. back-facing camera) and an output (e.g. an image file). In general, to capture a still image using the AVFoundation framework, you'll need to:
- Get an instance of
AVCaptureDevice
that represents the underlying input device such as the back-facing camera - Create an instance of
AVCaptureDeviceInput
with the device - Create an instance of
AVCaptureStillImageOutput
to manage the output to a still image - Use
AVCaptureSession
to coordinate the data flow from the input and the output - Create an instance of
AVCaptureVideoPreviewLayer
with the session to show a camera preview
If you still have questions at this point, no worries. The best way to learn any new concept is by trying it out - following along with the demo creation should help to clear up any confusion surrounding the AV Foundation framework.
Demo App
We're going to build a simple camera app that offers a full-screen experience and gesture-based controls. The app provides a minimalistic UI with a single capture button at the bottom of the screen. Users can swipe up the screen to switch between the front-facing and back-facing cameras. The camera offers up to 5x digital zoom. Users can swipe the screen from left to right to zoom in or from right to left to zoom out.
When the user taps the capture button, it should capture the photo in full resolution. Optionally, the user can save to the photo album.
To continue reading and access the full version of the book, please get the full copy here. You will also be able to access the full source code of the project.