【发布时间】:2016-11-03 02:17:09
【问题描述】:
我正在尝试将图像样本缓冲区中的一些元数据与图像一起保存。
我需要:
- 将图像旋转到元数据中的方向
- 从元数据中删除方向
- 将拍摄日期保存到元数据中
- 将该图像与元数据一起保存到文档目录中
我尝试从数据中创建 UIImage,但这会删除元数据。我尝试使用数据中的 CIImage 来保留元数据,但我无法旋转它然后将其保存到文件中。
private func snapPhoto(success: (UIImage, CFMutableDictionary) -> Void, errorMessage: String -> Void) {
guard !self.stillImageOutput.capturingStillImage,
let videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo) else { return }
videoConnection.fixVideoOrientation()
stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
(imageDataSampleBuffer, error) -> Void in
guard imageDataSampleBuffer != nil && error == nil else {
errorMessage("Couldn't snap photo")
return
}
let data = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
let metadata = CMCopyDictionaryOfAttachments(nil, imageDataSampleBuffer, CMAttachmentMode(kCMAttachmentMode_ShouldPropagate))
let metadataMutable = CFDictionaryCreateMutableCopy(nil, 0, metadata)
let utcDate = "\(NSDate())"
let cfUTCDate = CFStringCreateCopy(nil, utcDate)
CFDictionarySetValue(metadataMutable!, unsafeAddressOf(kCGImagePropertyGPSDateStamp), unsafeAddressOf(cfUTCDate))
guard let image = UIImage(data: data)?.fixOrientation() else { return }
CFDictionarySetValue(metadataMutable, unsafeAddressOf(kCGImagePropertyOrientation), unsafeAddressOf(1))
success(image, metadataMutable)
}
}
这是我保存图像的代码。
func saveImageAsJpg(image: UIImage, metadata: CFMutableDictionary) {
// Add metadata to image
guard let jpgData = UIImageJPEGRepresentation(image, 1) else { return }
jpgData.writeToFile("\(self.documentsDirectory)/image1.jpg", atomically: true)
}
【问题讨论】: