Swift:如何从AVFoundation拍摄的照片中删除EXIF数据?

Far*_*had 2 exif avfoundation ios swift

我试图摆脱AVFoundation拍摄的照片中的EXIF数据,我怎么能在swift(2)首选中做到这一点,Objective-C也没关系,我知道如何将代码转换为swift.

为什么? 我已经完成了我的研究,我看到很多着名的社交媒体(Reddit Source等等)确实删除了EXIF数据用于身份和其他目的.

如果您认为这是重复的帖子,请阅读我要求的内容并提供链接.谢谢.

Gab*_*ier 11

我的答案基于前一个问题.我修改了代码以使用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;
  }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以简单地做这样的事情:

let imageData = ImageHelper.removeExifData(UIImagePNGRepresentation(image))
Run Code Online (Sandbox Code Playgroud)

在我的示例中,我删除了旋转和EXIF数据.如果您需要删除其他任何内容,可以轻松搜索密钥.只需对生成的数据进行额外检查,因为它是可选的.

  • 您将需要在 ImageHelper 类中导入 ImageIO (2认同)