我试图摆脱使用 AVFoundation 拍摄的照片中的 EXIF 数据,如何在swift(2)中执行此操作首选,Objective-C也没关系,我知道如何将代码转换为swift。
为什么吗 我完成了我的研究,我发现许多着名的Social Media(Reddit Source等等)确实删除了EXIF数据以用于身份和其他目的。
如果您认为这是重复的帖子,请阅读我提出的问题并提供链接。谢谢。
答案 0 :(得分:11)
我的回答很基于this previous question。我修改了代码以使用Swift 2.0。
let imageData = ImageHelper.removeExifData(UIImagePNGRepresentation(image))
然后你可以简单地做这样的事情:
_
在我的示例中,我删除了旋转和EXIF数据。如果您需要删除其他任何内容,可以轻松搜索密钥。只需对生成的数据进行额外检查,因为它是可选的。
答案 1 :(得分:1)
你有UIImage吗? 然后,您可以将UIImage转换为数据并将其再次保存到图像,新图像将不具有任何EXIF数据
Swift 3
let imageData:Data = UIImagePNGRepresentation(image!)!
func saveToPhotoLibrary_iOS9(data:NSData, completionHandler: @escaping (PHAsset?)->()) {
var assetIdentifier: String?
PHPhotoLibrary.requestAuthorization { (status:PHAuthorizationStatus) in
if(status == PHAuthorizationStatus.authorized){
PHPhotoLibrary.shared().performChanges({
let creationRequest = PHAssetCreationRequest.forAsset()
let placeholder = creationRequest.placeholderForCreatedAsset
creationRequest.addResource(with: PHAssetResourceType.photo, data: data as Data, options: nil)
assetIdentifier = placeholder?.localIdentifier
}, completionHandler: { (success, error) in
if let error = error {
print("There was an error saving to the photo library: \(error)")
}
var asset: PHAsset? = nil
if let assetIdentifier = assetIdentifier{
asset = PHAsset.fetchAssets(withLocalIdentifiers: [assetIdentifier], options: nil).firstObject//fetchAssetsWithLocalIdentifiers([assetIdentifier], options: nil).firstObject as? PHAsset
}
completionHandler(asset)
})
}else {
print("Need authorisation to write to the photo library")
completionHandler(nil)
}
}
}
答案 2 :(得分:0)
已接受答案的 Swift 5 版本:
extension Data {
func byRemovingEXIF() -> Data? {
guard let source = CGImageSourceCreateWithData(self as NSData, nil),
let type = CGImageSourceGetType(source) else
{
return nil
}
let count = CGImageSourceGetCount(source)
let mutableData = NSMutableData()
guard let destination = CGImageDestinationCreateWithData(mutableData, type, count, nil) else {
return nil
}
let exifToRemove: CFDictionary = [
kCGImagePropertyExifDictionary: kCFNull,
kCGImagePropertyGPSDictionary: kCFNull,
] as CFDictionary
for index in 0 ..< count {
CGImageDestinationAddImageFromSource(destination, source, index, exifToRemove)
if !CGImageDestinationFinalize(destination) {
print("Failed to finalize")
}
}
return mutableData as Data
}
}