Dan Hixon Dan Hixon - 1 year ago 227
Swift Question

iOS 10 Save RAW Photo to camera roll

I have the following code to save a RAW image as a JPEG to the camera roll but it has three problems: 1) it is a mirror image, 2) it is rotated 90 degrees, and 3) it is low resolution.

// In my PhotoCaptureDelegate
func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingRawPhotoSampleBuffer rawSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {

if ( rawSampleBuffer != nil) {
let temporaryDNGFileURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(resolvedSettings.uniqueID)lld.dng")!
let temporaryJPGFileURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("\(resolvedSettings.uniqueID)lld.jpg")!
let imageData = AVCapturePhotoOutput.dngPhotoDataRepresentation(forRawSampleBuffer: rawSampleBuffer!, previewPhotoSampleBuffer: previewPhotoSampleBuffer)

try! imageData?.write(to: temporaryDNGFileURL)
PHPhotoLibrary.requestAuthorization({ (status) in
if status == .authorized {

let options = PHAssetResourceCreationOptions()
options.shouldMoveFile = true
//Write Raw:
PHAssetCreationRequest.forAsset().addResource(with: .photo, fileURL: temporaryDNGFileURL, options: options)

// Process Raw to JPG
if let ciRawFilter = CIFilter(imageData: imageData! as Data, options: nil) {
let cs = CGColorSpace(name: CGColorSpace.displayP3)!
do {
try self.contextForSaving.writeJPEGRepresentation(of: ciRawFilter.outputImage!, to: temporaryJPGFileURL, colorSpace: cs, options: [ kCGImageDestinationLossyCompressionQuality as String: 1.0])
PHAssetCreationRequest.forAsset().addResource(with: .photo, fileURL: temporaryJPGFileURL, options: options)
} catch _ {
print("error with jpeg conversion")

}, completionHandler: { [unowned self] success, error in
if let error = error {
print("Error occurered while saving photo to photo library: \(error)")
} else {
print("Raw photo written to photo library.")

if FileManager.default.fileExists(atPath: temporaryDNGFileURL.path) {
do {
(try FileManager.default.removeItem(at: temporaryDNGFileURL))
catch _ {
print("could not remove temporary file")
else {
} else {
print("Error capturing photo: \(error)")

I know how to capture the JPEG directly but I am trying to apply some custom filters to the RAW data first.

enter image description here

Please help if you can, thanks in advance!

Answer Source

I needed to specify the kCGImageSourceTypeIdentifierHint. It was just grabbing the jpeg thumbnail representation from the DNG data instead of looking for the actual DNG data. Now that I have this option in place the JPEG file is full-sized and correctly oriented.

let imageData = AVCapturePhotoOutput.dngPhotoDataRepresentation(
                forRawSampleBuffer: rawSampleBuffer!, 
                previewPhotoSampleBuffer: previewPhotoSampleBuffer)
let rawOptions = [String(kCGImageSourceTypeIdentifierHint): "com.adobe.raw-image"]
if let ciRawFilter = CIFilter(imageData: imageData! as Data, options: rawOptions) {
  // raw Filters and stuff go here, then handle outputImage
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download