信息:使用
Swift和CGImageSourceCreateWithURL函数.
我正在尝试从URL加载文件,然后编辑包含该特定照片中所有数据的字典.
let url = NSURL(string: "http://jwphotographic.co.uk/Images/1.jpg") let imageSource = CGImageSourceCreateWithURL(url,nil) let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource,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..
我已经做了很多研究和测试,我根本无法解决我在访问这本词典中的元素时所做的错误 – 有人能举例说明如何将变量设置为字典中的“Model”元素?
解决方法
在Swift 3.0中,我找到了以下解决方案
let url = NSURL(string: "http://jwphotographic.co.uk/Images/1.jpg") let imageSource = CGImageSourceCreateWithURL(url,nil) let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource,nil) as Dictionary? let exifDict = imageProperties?[kCGImagePropertyExifDictionary]
现在您可以通过例如访问exif-Tags
let dateTimeOriginal = exifDict?[kCGImagePropertyExifDateTimeOriginal] Swift.print("dateTimeOriginal: \(dateTimeOriginal)")
您将获得可选值,如果有值或零,则必须进行测试.有关可用属性常量的列表,请查看apple documentation.