【发布时间】:2018-10-09 06:52:41
【问题描述】:
我正在尝试编写一个可以同时拍摄 RAW 和 JPEG 图像并将它们保存到相机胶卷的照片应用程序。函数 jpegPhotoDataRepresentation 和 dngPhotoDataRepresentation 似乎是我找到的所有示例的关键,但是这两个在 iOS 11 中都已弃用,并且在“capturePhoto”之后保存的功能现在是
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
我能够找到一个工作的 RAW iOS11 应用程序的主要示例是这样的: https://ubunifu.co/swift/raw-photo-capture-sample-swift-4-ios-11 这可行,但是它只拍摄 RAW 并且保存很笨拙,因为它不在相机胶卷上。
我已经更改了我的照片设置,以允许使用这条线进行原始和处理后的拍摄
photoSettings = AVCapturePhotoSettings(rawPixelFormatType: availableRawFormat.uint32Value, processedFormat: [AVVideoCodecKey : AVVideoCodecType.jpeg])
但是,一旦我实际拍摄了照片,我就不知道如何访问已处理的格式数据。 fileDataRepresentation 似乎是访问 dng 内容的唯一方法,但是没有办法单独获取 jpeg 吗?我从 Apple pre-iOS11 中找到的代码建议使用 PHPhotoLibrary 并添加一个资源,但这需要一个数据表示,除了作为 dng 文件之外我无法访问,当保存到库时只是白色的,因为该库无法处理 RAW 文件。这是我的 photoOutput 代码,以防万一。
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
let dir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as String
let formatter = DateFormatter()
formatter.dateFormat = "yyyyMMddHHmmss"
formatter.locale = Locale.init(identifier: "en_US_POSIX")
let filePath = dir.appending(String(format: "/%@.dng", formatter.string(from: Date())))
let dngFileURL = URL(fileURLWithPath: filePath)
let dngData = photo.fileDataRepresentation()!
do {
try dngData.write(to: dngFileURL, options: [])
} catch {
print("Unable to write DNG file.")
return
}
PHPhotoLibrary.shared().performChanges( {
let creationRequest = PHAssetCreationRequest.forAsset()
let creationOptions = PHAssetResourceCreationOptions()
creationOptions.shouldMoveFile = true
//dngData is the problem, this should be the jpeg representation
creationRequest.addResource(with: .photo, data: dngData, options: nil)
//This line works fine, the associated file is the correct RAW file, but the jpeg preview is garbage
creationRequest.addResource(with: .alternatePhoto, fileURL: dngFileURL, options: creationOptions)
}, completionHandler: nil)
}
【问题讨论】:
-
稍后会有更多答案,但要开始:查看 Apple 文档中的 Capturing Photos in RAW Format。
标签: ios swift camera jpeg ios11