iOS 10将RAW照片保存到相机胶卷

时间:2016-08-31 21:13:07

标签: swift ios10

我有以下代码将RAW图像作为JPEG保存到相机胶卷,但它有三个问题:1)它是镜像,2)它旋转90度,3)它是低分辨率。

// 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 {
          PHPhotoLibrary.shared().performChanges({

            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")
                }
              }
              self.didFinish()
            }
          )
        }
        else {
          self.didFinish()
        }
    })
  } else {
    print("Error capturing photo: \(error)")
  }
}

我知道如何直接捕获JPEG,但我首先尝试将一些自定义过滤器应用于RAW数据。

enter image description here

如果可以,请提供帮助,提前谢谢!

2 个答案:

答案 0 :(得分:3)

我测试了以下代码,以jpeg格式保存原始滤镜的输出图像,方向和分辨率正确。根据您的要求更改jpeg质量和输出颜色空间。希望这会对你有所帮助。

    guard let opCiImage:CIImage = rawFilter!.outputImage else {
        print("Error in creating raw filter output")
        return
    }
    let dumpingContext:CIContext = CIContext(options: [kCIContextCacheIntermediates:false,
                                                       kCIContextPriorityRequestLow:false])



    //Convert to CGImage and then dump into a jpeg
    let opCgImage = dumpingContext.createCGImage(opCiImage,
                                                   from: opCiImage.extent,
                                                   format: kCIFormatARGB8,
                                                   colorSpace: CGColorSpace(name: CGColorSpace.sRGB),
                                                   deferred: false)
    let uImage = UIImage(cgImage: opCgImage!)
    guard let opImgData:Data = UIImageJPEGRepresentation(uImage, 0.95) else { return }

请在评论中告诉我反馈。

答案 1 :(得分:2)

我需要指定kCGImageSourceTypeIdentifierHint。它只是从DNG数据中抓取JPEG缩略图表示,而不是查找实际的DNG数据。现在我已经有了这个选项,JPEG文件是完整大小且正确定位的。

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
}