Swift访问EXIF字典

use*_*554 7 exif image cgimage ios swift

信息:使用Swift和CGImageSourceCreateWithURL函数.

我正在尝试从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)
Run Code Online (Sandbox Code Playgroud)

尽管图像属性打印的所有数据都是预期的,但不管我从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..
Run Code Online (Sandbox Code Playgroud)

我已经做了很多研究和测试,我根本无法解决我在访问这本词典中的元素时所做的错误 - 有人能举例说明如何将变量设置为字典中的"Model"元素?

Eri*_*ter 12

在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]
Run Code Online (Sandbox Code Playgroud)

现在您可以通过例如访问exif-Tags

let dateTimeOriginal = exifDict?[kCGImagePropertyExifDateTimeOriginal]
Swift.print("dateTimeOriginal: \(dateTimeOriginal)")
Run Code Online (Sandbox Code Playgroud)

您将获得可选值,如果有值或零,则必须进行测试.有关可用属性常量的列表,请查看apple文档.


Cor*_*tex 8

要获取Exif参数,请执行以下操作:

    let filePath_ = "file:///Users/pfernandes/Documents/softwareDevelopment/PhotoLine/TestData/IMG_1243.JPG"
    let fileURL = NSURL(string:filePath_)
    let myImage = CGImageSourceCreateWithURL(fileURL!,nil)

    let imageDict = CGImageSourceCopyPropertiesAtIndex(myImage!, 0, nil)! as NSDictionary;
    let tiffModel_ = imageDict.value(forKey: "{TIFF}")

    let cameraMake = (tiffModel_ as AnyObject).value(forKey: kCGImagePropertyTIFFMake as String)
    let cameraModel = (tiffModel_ as AnyObject).value(forKey: kCGImagePropertyTIFFModel as String)

    let exifDict_ = imageDict.value(forKey: "{Exif}") as! NSDictionary
    let dateTimeOriginal = exifDict_.value(forKey:kCGImagePropertyExifDateTimeOriginal as String) as! NSString
    let exposure         = exifDict_.value(forKey:kCGImagePropertyExifExposureTime as String)

    print ("\(String(describing: cameraMake)) \(String(describing: cameraModel)) \(dateTimeOriginal) \(exposure!)")
Run Code Online (Sandbox Code Playgroud)