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

标签 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

如果可以请帮忙,在此先感谢!

最佳答案

我已经测试了以下代码,以正确的方向和分辨率以 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 }

请在评论中让我知道反馈。

关于swift - iOS 10 将 RAW 照片保存到相机胶卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39259094/

相关文章:

ios - iOS 上可能的最大字体大小

swift - 检查 Any.Type 是否符合 Swift 中的协议(protocol)

ios - 更改导航栏底部边框颜色 Swift

ios - 关闭以编程方式呈现在嵌套在 NavigationController 中的 ViewController 中的 ViewController 崩溃,但并非每次都如此

ios - EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000200 向图像添加过滤器时

ios - 如何将 Int 数组转换为 String? Swift 中的数组

ios - 我无法再在我的设备上测试我的应用程序

ios - UITextView 文本被截断 - iOS 10

ios - 如何在 Collection View 中添加 indexTitles? (iOS 10+)

ios - 位置权限对话框在 iOS 10 中提示很多时间