Swift:如何从AVFoundation拍摄的照片中删除EXIF数据?
我试图摆脱从AVFoundation拍摄的图片的EXIF数据,我怎么能在swift(2) 首选 ,Objective-C也可以,我知道如何将代码转换为swift。
为什么? 我已经完成了我的研究,并且我看到很多着名的社交媒体 (Reddit Source等等)为了身份识别和其他目的删除了EXIF数据。
如果您认为这是重复的post,请阅读我要求的内容并提供链接。 谢谢。
我的回答是基于这个前一个问题 。 我修改了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数据。 如果您需要删除任何其他内容,您可以轻松search密钥。 只需对生成的数据进行额外的检查,因为它是可选的。
你有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) } } }