Get minimally processed data from the camera's image sensor for custom processing.
By default, iOS and a device's image signal processor perform a number of operations on the data from a camera's image sensor to create a displayable image. These manipulations include dead pixel interpolation, demosaicing, white balance, noise reduction, tone compensation, and sharpening. This processing occurs as part of capture, creating a high-quality image compressed for easy storage and display.
Sometimes it's useful to postpone such processing. Capturing in a RAW data format lets you get virtually unadulterated data from the camera's image sensor. You can't directly display a RAW-format image, and it requires much more data storage than a finished HEIF/HEVC or JPEG file. However, you can process the RAW data to modify the image with much greater fidelity than is possible by postprocessing—for example, RAW formats store much more dynamic range than processed formats, so a user can choose different exposure levels for an image after shooting.
In iOS, capturing in RAW format and saving results requires only minor changes to the basic photography workflow covered in Capturing Still and Live Photos.
Choose RAW Format Settings
Typical RAW shooting workflows capture both a RAW format image and a processed (HEIF/HEVC or JPEG) image for each shot: saving the RAW data allows the user to process it later, but keeping a processed copy makes previewing and general use more convienent. To shoot in both formats, use
photo to create an
AVCapture object before capture.
The photo output calls your delegate's
capture method at least once for each format you've requested, and possibly additional times depending on your capture settings. For example, if you request RAW+HEIF capture in a three-exposure bracket, the photo output calls your delegate's
did method six times (2 formats × 3 exposures), providing six
To keep track of multiple results, compare the
photo from each photo to the
expected of your resolved settings. When those numbers are equal, you've received all results from the capture. Alternatively, hold all results until the photo output calls your
capture method, and save all output in that method.
To save output, use the photo object's
file method to get the RAW image data packaged in the industry-standard DNG file format (along with capture metadata and any auxiliary images you requested in capture settings).
Save RAW and Processed Image Data to the Photos Library
To save RAW images in the user's Photos library, you'll need to create a single Photos asset that associates the RAW data (saved in DNG format) with a processed version (HEIF/HEVC or JPEG) for use by apps that can't display RAW images. To do so, use the
PHAsset class, specifying the processed image as the asset's main
PHAsset resource and the DNG version as an
PHAsset resource. As in Saving Captured Photos, you'll need to wrap that request in a
PHPhoto change block, and first make sure that your app has the user's permission to access Photos.