avatarQuentin Fasquel

Summary

The context introduces a SwiftUI library called "Capture" for iOS, which simplifies the process of using the camera in SwiftUI applications.

Abstract

The context discusses the history of using the camera with SwiftUI in iOS, starting from iOS 14 and the introduction of PHPickerViewController. It then introduces a SwiftUI library called "Capture" as an elegant alternative to the existing solutions. The library uses AVFoundation to provide a more streamlined solution for developers who have embraced SwiftUI. The context also provides an example of the minimum code necessary to have a camera preview and take a picture using the Capture library. The library supports camera features such as Flash, Torch, Focus, and Exposure and is currently only available for iOS, but there are plans to make it available for other platforms such as macOS, tvOS, and visionOS.

Bullet points

  • iOS 14 introduced PHPickerViewController as the preferred UIKit view controller to handle the photo library aspect, leaving UIImagePickerController as the only way to access the camera.
  • iOS 16 brought an elegant SwiftUI alternative to PHPickerViewController, PhotosPicker, part of PhotosUI.
  • iOS 17 did not introduce any SwiftUI features that could replace UIImagePickerController for taking pictures or recording videos.
  • The context introduces a SwiftUI library called "Capture" for iOS, which simplifies the process of using the camera in SwiftUI applications.
  • The Capture library uses AVFoundation to provide a more streamlined solution for developers who have embraced SwiftUI.
  • The context provides an example of the minimum code necessary to have a camera preview and take a picture using the Capture library.
  • The Capture library supports camera features such as Flash, Torch, Focus, and Exposure.
  • The Capture library is currently only available for iOS, but there are plans to make it available for other platforms such as macOS, tvOS, and visionOS.

iOS — Using the camera with SwiftUI

Sharing is caring

A little history

Until iOS 14, UIImagePickerController used to be the view controller doing it all when it came to taking pictures, recording videos or even choosing items from the photo library.

iOS 14 brought PHPickerViewController as the preferred UIKit view controller to handle the photo library aspect, leaving UIImagePickerController with a name that doesn’t quite match its main intent.

iOS 16 brought an elegant SwiftUI alternative to PHPickerViewController, PhotosPicker, part of PhotosUI.

iOS 17 did not introduce any SwiftUI features that could replace UIImagePickerController for taking pictures or recording videos.

Introducing Capture, for SwiftUI

Reducing iOS to having UIImagePickerController as the only way to access the camera would be a mistake.

Apple offers great SDKs. With the Photos framework, you could develop your own implementation of a photos picker. To take pictures or record videos, AVFoundation brings all the tools you may need to implement a personalized camera UI.

AVCaptureSession is great, but it does involve a considerable amount of setup. As developers who have embraced SwiftUI, there is a demand for a more streamlined solution. And that’s >>> Capture <<<

CameraView

This tutorial by Apple was the initial inspiration, but also a sign SwiftUI lacks a helper around camera capabilities..

The example above shows the minimum code necessary to have a camera preview and to take a picture. Obtaining the result as a UIImage is the straightforward solution that allows to display the captured photo easily, and to save it to a file using pngData()or jpegData(compressionQuality:) .

Camera, PhotoSettings, VideoSettings, etc.

AVFoundation offers obviously much more than that and Capture will attempt to cover most of it, step by step.

CameraView comes with closure to describe a camera view overlay. This overlay should vary according to the current camera authorization, and it carries the two environment actions takePicture & recordVideo .

Environment values such as recordingAudioSettings and recordingVideoSettings can be overridden to affect the photo and video capture. These settings are exposed as Swift-friendly structs instead of dictionaries, providing an easier API discovery.

By default, CameraView uses a default instance of the Camera class. Camera is a wrapper of AVCaptureSession. A simple use of the Capture library doesn’t require to access it, yet it is possible to pass this instance to the CameraView from a parent, making it possible to access camera features outside from the CameraView overlay.

Soon supporting camera features such as Flash, Torch, Focus and Exposure.

Multiple CameraViews

Although AVCaptureMultiCamSession is available since iOS 13, this wasn’t the initial goal of the Capture library to allow for multiple camera views and isn’t currently possible. But it is worth considering to develop this feature in a near future.

Platform support

Since UIImagePickerController was the SwiftUI replacement in mind for Capture’s CameraView, it isn’t currently available to macOS. However SwiftUI intends to provide APIs that are available to most Apple platforms and AVCaptureSession is available to macOS, tvOS and visionOS. Therefore this will be the next focus for this library.

Let me know if you find this library useful. Feel free to contact me for specific features that you’d want to see. Also you can support me : https://www.buymeacoffee.com/quentinf

Swiftui
Camera
iOS
Avfoundation
Vi̇deo
Recommended from ReadMedium