我需要:
>将图像旋转到元数据的方向
>从元数据中删除方向
>将日期保存到元数据中
>将包含元数据的图像保存到文档目录
我试过从数据创建一个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])}总结
以上是内存溢出为你收集整理的ios – Swift:自定义相机使用图像保存修改后的元数据全部内容,希望文章能够帮你解决ios – Swift:自定义相机使用图像保存修改后的元数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)