【发布时间】:2017-08-30 23:13:22
【问题描述】:
我正在使用以下代码向 iPhone 设备上的图像添加 exif 数据:
func testPhoto(identifier: String) {
let result =
PHAsset.fetchAssets(withLocalIdentifiers: [identifier], options: nil)
PHCachingImageManager
.default()
.requestImageData(for: result.firstObject!, options: nil) {
(data, uti, orientation, info) in
_ = self.addExif(data: data!)
}
}
func addExif(data: Data) -> NSData? {
let selectedImageSourceRef =
CGImageSourceCreateWithData(data as CFData, nil)!
let imagePropertiesDictionaryRef =
CGImageSourceCopyPropertiesAtIndex(selectedImageSourceRef, 0, nil)
var imagePropsDictionary: [String: Any] =
imagePropertiesDictionaryRef as! [String : Any]
var exifData = [String: Any]()
let newImageData = NSMutableData()
let imageDestination =
CGImageDestinationCreateWithData(newImageData, kUTTypeJPEG, 1, nil)!
exifData[kCGImagePropertyExifDateTimeOriginal as String] = NSDate()
imagePropsDictionary[kCGImagePropertyExifDictionary as String] = exifData
CGImageDestinationAddImageFromSource(
imageDestination,
selectedImageSourceRef,
0,
imagePropsDictionary as CFDictionary)
if !CGImageDestinationFinalize(imageDestination) {
NSLog("Could not finalize")
}
return newImageData
}
该方法在尝试完成目标数据源 (CGImageDestinationFinalize) 时崩溃。
应用程序因内存压力而终止。这发生在大图像(20,000x20,000 像素)上。普通图片通过这个方法就好了。
有没有什么办法可以在不造成内存压力的情况下给图片添加EXIF信息?
谢谢!
【问题讨论】:
标签: ios image exif core-image photos