ios - 快速访问 EXIF 字典

标签 ios image swift exif cgimage

信息:使用 Swift 和 CGI​​mageSourceCreateWithURL 函数。

我正在尝试从 URL 加载文件,然后编辑包含该特定照片的所有数据的字典。

这是 .swift 文件中的代码。

    let url = NSURL(string: "http://jwphotographic.co.uk/Images/1.jpg")
    let imageSource = CGImageSourceCreateWithURL(url, nil)
    let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as Dictionary

    println(imageProperties)

    //this is an example
    let aperture = imageProperties[kCGImagePropertyGPSLatitude] as! NSNumber!

    /*
    //these are all being defined as nil
    //Load the ones from the exif data of the file
    let lensUsed = imageProperties[kCGImagePropertyExifFocalLength]
    let aperture = imageProperties[kCGImagePropertyExifApertureValue] as!
    let isoSpeed = imageProperties[kCGImagePropertyExifISOSpeedRatings] as! NSNumber
    let latitude = imageProperties[kCGImagePropertyGPSLatitude] as! NSNumber
    let longitude = imageProperties[kCGImagePropertyGPSLongitude] as! NSNumber
    let shutterSpeed = imageProperties[kCGImagePropertyExifShutterSpeedValue] as! NSNumber
    let cameraName = imageProperties[kCGImagePropertyExifBodySerialNumber] as! NSNumber
    */

    println(aperture)

尽管图像属性按预期打印所有数据,但无论我尝试从 imageProperties 字典中提取什么 - 它始终返回为 null - 例如示例中的 'aperture'。 imageProperties 打印为;

[{TIFF}: {
     Artist = JOHN;
     Copyright = "johnrwatson0@gmail.com";
     DateTime = "2015:07:31 21:07:05";
     Make = Canon;
     Model = "Canon EOS 7D Mark II";
     ResolutionUnit = 2;
     Software = "Adobe Photoshop Lightroom 6.0 (Macintosh)";
     XResolution = 72;
     YResolution = 72;
}, {IPTC}: {
     Byline =     (
       JOHN
     );
     CopyrightNotice = etc.. etc..

我已经做了很多研究和测试,但我根本无法弄清楚我在访问这本字典中的元素时做错了什么 - 有人能给我一个例子,我如何将变量设置为“模型”元素在字典里面?

最佳答案

在 Swift 3.0 中我找到了以下解决方案

let url = NSURL(string: "http://jwphotographic.co.uk/Images/1.jpg")
let imageSource = CGImageSourceCreateWithURL(url, nil)
let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as Dictionary?
let exifDict = imageProperties?[kCGImagePropertyExifDictionary]

现在您可以通过例如访问 exif 标签

let dateTimeOriginal = exifDict?[kCGImagePropertyExifDateTimeOriginal]
Swift.print("dateTimeOriginal: \(dateTimeOriginal)")

您将获得可选值,您必须测试是否有值或 nil。有关可用属性常量的列表,请查看 apple documentation .

关于ios - 快速访问 EXIF 字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31888319/

相关文章:

ios - 合并两个相同类型的对象

ios - 如何在 Swift 中使用我自己的函数创建一个文件

ios - 如何检测 UITextField 上的点击?

ios - Flex 自定义皮肤无法调出 IOS 软键盘

javascript - 如果互联网速度高加载图像或根本不加载

PHP GD 调色板颜色

swift - 将 SwiftUI 与自定义发布者结合起来 - 使用 .assign 订阅者时出现意外行为

python - Blender UV 纹理图像未使用 Python API 更新

ios - 来自外部源的 iframe 内容未加载到 iOS 上的本地 HTML 文件中

ios - 在 Xcode Swift 中从 Assets.xcassets 中的文件夹访问图像