Swift:如何从使用AVFoundation拍摄的照片中删除EXIF数据? [英] Swift: How to Delete EXIF data from picture taken with AVFoundation?

查看:89
本文介绍了Swift:如何从使用AVFoundation拍摄的照片中删除EXIF数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图摆脱使用 AVFoundation 拍摄的照片中的 EXIF 数据,如何迅速(2)首选/em>,Objective-C也可以,我知道如何将代码转换为swift.

I'm trying to get rid of the EXIF data from a picture taken with AVFoundation, How can I do this in swift (2) preferred, Objective-C is okay too, I know how to convert the code to swift.

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

Why? I have done my research and I see a lot of famous Social Media (Reddit Source and many more) do remove EXIF data for identity purposes and other purposes.

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

If you think this is duplicate post, please read what I'm asking and provide link. Thank you.

推荐答案

我的答案很大程度上基于

My answer is based a lot on this previous question. I adapted the code to work on 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;
  }
}

然后,您可以简单地执行以下操作:

Then you can simply do something like this:

let imageData = ImageHelper.removeExifData(UIImagePNGRepresentation(image))

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

In my example, I remove the rotation and the EXIF data. You can easily search the keys if you need anything else removed. Just make extra checks on the data generated as it is an optional.

这篇关于Swift:如何从使用AVFoundation拍摄的照片中删除EXIF数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆