Swift访问EXIF字典 [英] Swift accessing EXIF Dictionary
问题描述
信息:使用Swift和CGImageSourceCreateWithURL函数。
Info: Using Swift and the CGImageSourceCreateWithURL function.
我正在尝试从URL加载文件,然后编辑包含该特定数据的所有数据的字典照片。
I am attempting to load a file from a URL and then edit a dictionary which has all the data from that particular photo.
这是.swift文件中的代码。
This is the code from the .swift file.
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 - 例如示例中的光圈。 imageProperties打印为;
Even though image properties prints all the data as would be expected, no-matter what I have attmpted to extract from the imageProperties dictionary - it is always returned as null - such as 'aperture' in the example. The imageProperties prints as;
[{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..
我做了很多研究和测试我根本无法解决我在访问这个字典中的元素时所做的错误 - 有人能举例说明如何将变量设置为字典中的Model元素吗?
I have done a lot of research and testing and I simply cannot work out what I'm doing wrong to access the elements in this dictionary - Could someone give me an example how I would set a variable as the "Model" element inside the dictionary?
推荐答案
在Swift 3.0中我找到了以下解决方案
In Swift 3.0 I found the following solution
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-Tags例如
Now you can access the exif-Tags by for example
let dateTimeOriginal = exifDict?[kCGImagePropertyExifDateTimeOriginal]
Swift.print("dateTimeOriginal: \(dateTimeOriginal)")
您将获得可选值,如果有值或零,则必须进行测试。有关可用属性常量的列表,请查看 apple文档。
You will get optional values and you have to test, if there are values or nil. For a list of available property constants look at the apple documentation.
这篇关于Swift访问EXIF字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!