我正在使用以下代码从图片中删除元数据:
fileprivate func removeMetadataFromData(data: Data) -> NSMutableData? {
guard let source = CGImageSourceCreateWithData(data as CFData, nil) else {return nil}
guard let type = CGImageSourceGetType(source) else {return nil}
let count = CGImageSourceGetCount(source)
let mutableData = NSMutableData(data: data)
guard let destination = CGImageDestinationCreateWithData(mutableData, type, count, nil) else {return nil}
let newProperties: CFDictionary = [String(kCGImagePropertyExifDictionary) : kCFNull, String(kCGImagePropertyGPSDictionary): kCFNull] as CFDictionary
for i in 0..<count {
CGImageDestinationAddImageFromSource(destination, source, i, properties)
}
guard CGImageDestinationFinalize(destination) else {return nil}
return mutableData
}
这应该只从原始照片中删除Exif和Location字典,但同时也删除了我想要保留的MakerApple字典。
我尝试像这样将其添加回去:
let oldImageProperties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as Dictionary?
let makerAppleData = imageProperties?[kCGImagePropertyMakerAppleDictionary] as? NSMutableDictionary
let newProperties: CFDictionary = [String(kCGImagePropertyExifDictionary) : kCFNull, String(kCGImagePropertyGPSDictionary): kCFNull, String(kCGImagePropertyMakerAppleDictionary):makerAppleData] as CFDictionary
,但不会将其添加回去。有解决方案吗?