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

标签 ios swift avfoundation exif

我正试图从使用 AVFoundation 拍摄的照片中删除 EXIF 数据,如何快速完成此操作 (2) preferred,Objective-C 也可以,我知道如何将代码转换为 swift。

为什么? 我做了我的研究,我看到了很多著名的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 数据。如果您需要删除任何其他内容,您可以轻松搜索 key 。只需对生成的数据进行额外检查,因为它是可选的。

关于ios - Swift:如何从使用 AVFoundation 拍摄的照片中删除 EXIF 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32788736/

相关文章:

iphone - 重新创建预览歌曲的 iTunes(iOS 应用程序)tableViewCell

iphone - UILocalNotification 不会触发

ios - 如何改变UItableview的数据?

ios - 使用 AVAudioSequencer 将 MIDI 发送到第三方 AUv3 乐器

ios - 如果元素符合给定协议(protocol),则扩展数组以符合协议(protocol)

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

ios - 在 Swift 中使用动态键解析 JSON

ios - 相机预览层不显示 Swift 4

swift - iOS 10 打破自定义 CIFilter

swift - NSTemporaryDirectory() 没有空格