我已经获得了图像的所有信息:
let imageRef:CGImageSourceRef = CGImageSourceCreateWithURL(url,nil).takeUnretainedValue()let imageDict:CFDictionaryRef = CGImageSourcecopyPropertIEsAtIndex(imageRef,nil).takeUnretainedValue()
该词典包含以下信息:
{ colorModel = Gray; DPIHeight = 300; DPIWIDth = 300; Depth = 1; OrIEntation = 1; PixelHeight = 4167; PixelWIDth = 4167; "{Exif}" = { colorSpace = 65535; DateTimeDigitized = "2014:07:09 20:25:49"; PixelXDimension = 4167; PixelYDimension = 4167; }; "{TIFF}" = { Compression = 1; DateTime = "2014:07:09 20:25:49"; OrIEntation = 1; PhotometricInterpretation = 0; ResolutionUnit = 2; Software = "Adobe Photoshop CS6 (Macintosh)"; XResolution = 300; YResolution = 300; };}
现在我想用以下代码读取DPI的值,并且“__conversion”存在一些问题,我不明白.
let dpiH:NSNumber = CFDictionaryGetValue(imageDict,kCGImagePropertyDPIWIDth)
我做错了什么,怎样才能达到字典所需的值?
我发现通过将CFDictionary“转换”为Swift字典来访问属性要容易得多.let imageSource = CGImageSourceCreateWithURL(imageURL,nil)let imagePropertIEs = CGImageSourcecopyPropertIEsAtIndex(imageSource,nil) as Dictionarylet dpiWIDth = imagePropertIEs[kCGImagePropertyDPIWIDth] as NSNumber
Swift 2.0的快速更新(原谅所有if let – 我只是快速制作了这段代码):
import UIKitimport ImageIOclass VIEwController: UIVIEwController { overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() if let imagePath = NSBundle.mainBundle().pathForResource("test",ofType: "jpg") { let imageURL = NSURL(fileURLWithPath: imagePath) if let imageSource = CGImageSourceCreateWithURL(imageURL,nil) { if let imagePropertIEs = CGImageSourcecopyPropertIEsAtIndex(imageSource,nil) as Dictionary? { let pixelWIDth = imagePropertIEs[kCGImagePropertyPixelWIDth] as! Int print("the image wIDth is: \(pixelWIDth)") } } } }}总结
以上是内存溢出为你收集整理的macos – 使用swift读取CFDictionary中的值全部内容,希望文章能够帮你解决macos – 使用swift读取CFDictionary中的值所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)