Swift:如何从使用AVFoundation拍摄的照片中删除EXIF数据?
问题描述:
我想从与AVFoundation拍摄的照片,我怎样才能迅速(2)首选做到这一点摆脱EXIF的数据,Objective-C的还好过,我知道如何转换代码迅速。Swift:如何从使用AVFoundation拍摄的照片中删除EXIF数据?
为什么? 我已经完成了我的研究,并且看到很多着名的Social Media(Reddit源和更多)为了身份识别和其他目的删除EXIF数据。
如果您认为这是重复的帖子,请阅读我的要求并提供链接。谢谢。
答
我的回答基于很多this previous question。我修改了Swift 2.0的代码。
class ImageHelper {
static func removeExifData(data: NSData) -> NSData? {
guard let source = CGImageSourceCreateWithData(data, 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
}
// Check the keys for what you need to remove
// As per documentation, if you need a key removed, assign it kCFNull
let removeExifProperties: CFDictionary = [String(kCGImagePropertyExifDictionary) : kCFNull, String(kCGImagePropertyOrientation): kCFNull]
for i in 0..<count {
CGImageDestinationAddImageFromSource(destination, source, i, removeExifProperties)
}
guard CGImageDestinationFinalize(destination) else {
return nil
}
return mutableData;
}
}
然后,你可以简单地做这样的事情:
let imageData = ImageHelper.removeExifData(UIImagePNGRepresentation(image))
在我的例子,我删除了旋转和EXIF数据。如果您需要删除其他任何内容,您可以轻松搜索密钥。只需对生成的数据进行额外检查,因为它是可选的。
答
你有UIImage吗? 然后你可以的UIImage转换成数据并保存到图像再次新的图像不会有任何的EXIF数据
斯威夫特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)
}
}
}
你将需要进口的ImageIO在ImageHelper类 – Xitcod13
阀块这段代码斯威夫特3并设置kcfNull不会删除一个属性,但例如我可以通过设置'kCGImagePropertyOrientation'作为字符串:1'来将其更改为其他内容,并且结果的方向值为3. 但是我想删除任何exif数据,因为kcfNull没有任何想法加工? –
@MilosZikic你检查过文档了吗?有一段时间没有工作,但我知道它是写在文档中如何设置或删除值。 –