我试图保存图像样本缓冲区中的一些元数据以及图像.
我需要:
>将图像旋转到元数据的方向
>从元数据中删除方向
>将日期保存到元数据中
>将包含元数据的图像保存到文档目录
我试过从数据创建一个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,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) }
解决方法
我最终弄清楚如何让一切按照我需要的方式工作.对我帮助最大的事情是发现CFDictionary可以作为NSMutableDictionary投射.
这是我的最终代码:
如您所见,我在EXIF词典中为数字化日期添加了一个属性,并更改了方向值.
private func snapPhoto(success: (UIImage,NSMutableDictionary) -> Void,error) -> Void in guard imageDataSampleBuffer != nil && error == nil else { errorMessage("Couldn't snap photo") return } let data = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer) let rawMetadata = CMcopyDictionaryOfAttachments(nil,CMAttachmentMode(kCMAttachmentMode_ShouldPropagate)) let Metadata = CFDictionaryCreateMutablecopy(nil,rawMetadata) as NSMutableDictionary let exifData = Metadata.valueForKey(kCGImagePropertyExifDictionary as String) as? NSMutableDictionary exifData?.setValue(NSDate().toString("yyyy:MM:dd HH:mm:ss"),forKey: kCGImagePropertyExifDateTimeDigitized as String) Metadata.setValue(exifData,forKey: kCGImagePropertyExifDictionary as String) Metadata.setValue(1,forKey: kCGImagePropertyOrientation as String) guard let image = UIImage(data: data)?.fixOrientation() else { errorMessage("Couldn't create image") return } success(image,Metadata) } }
以及使用元数据保存图像的最终代码:
很多防守声明,我讨厌,但它比强行解缠更好.
func saveImage(withMetadata image: UIImage,Metadata: NSMutableDictionary) { let filePath = "\(self.documentsPath)/image1.jpg" guard let jpgData = UIImageJPEGRepresentation(image,1) else { return } // Add Metadata to jpgData guard let source = CGImageSourceCreateWithData(jpgData,nil),let uniformTypeIdentifier = CGImageSourceGetType(source) else { return } let finalData = NSMutableData(data: jpgData) guard let destination = CGImageDestinationCreateWithData(finalData,uniformTypeIdentifier,1,nil) else { return } CGImageDestinationAddImageFromSource(destination,source,Metadata) guard CGImageDestinationFinalize(destination) else { return } // Save image that Now has Metadata self.fileService.save(filePath,data: finalData) }
这是我更新的保存方法(与我在编写此问题时使用的完全相同,因为我已更新到Swift 2.3,但概念是相同的):
public func save(fileAt path: NSURL,with data: NSData) throws -> Bool { guard let pathString = path.absoluteString else { return false } let directory = (pathString as Nsstring).stringByDeletingLastPathComponent if !self.fileManager.fileExistsAtPath(directory) { try self.makeDirectory(at: NSURL(string: directory)!) } if self.fileManager.fileExistsAtPath(pathString) { try self.delete(fileAt: path) } return self.fileManager.createFileAtPath(pathString,contents: data,attributes: [NSFileProtectionKey: NSFileProtectionComplete]) }