Macos 用swift阅读cfd词典的价值

Macos 用swift阅读cfd词典的价值,macos,swift,core-graphics,key-value,core-foundation,Macos,Swift,Core Graphics,Key Value,Core Foundation,我刚从斯威夫特和可可开始。我正在尝试创建一个基本的应用程序来处理图像 我已经准备好了这张图片的所有信息: let imageRef:CGImageSourceRef = CGImageSourceCreateWithURL(url, nil).takeUnretainedValue() let imageDict:CFDictionaryRef = CGImageSourceCopyPropertiesAtIndex(imageRef, 0, nil).takeUnretainedValue()

我刚从斯威夫特和可可开始。我正在尝试创建一个基本的应用程序来处理图像

我已经准备好了这张图片的所有信息:

let imageRef:CGImageSourceRef = CGImageSourceCreateWithURL(url, nil).takeUnretainedValue()
let imageDict:CFDictionaryRef = CGImageSourceCopyPropertiesAtIndex(imageRef, 0, 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的值,但“\uu转换”有一些问题,我不明白

let dpiH:NSNumber = CFDictionaryGetValue(imageDict, kCGImagePropertyDPIWidth)

我做错了什么?如何获得字典的期望值?

我发现通过将CFDictionary“转换”为Swift字典来访问属性要容易得多

let imageSource = CGImageSourceCreateWithURL(imageURL, nil)
let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as Dictionary
let dpiWidth = imageProperties[kCGImagePropertyDPIWidth] as NSNumber

Swift 2.0的快速更新(请原谅所有的
if let
s-我刚刚快速编写了此代码):


您能在xCode中发布您收到的全部错误吗?它会停止编译,并出现以下错误:“无法为接受提供的参数的“\uuu转换”设置fing重载”上面的错误消息不是副本:“fing”.Sry,typo。应该是“查找”。手动键入,但如果您需要导航器的副本:
找不到接受所提供参数的“\uu转换”重载
我一直在寻找从图像中提取EXIF信息的答案。这就解决了问题。非常感谢。它不适用于Swift 2.0,因为
CFDictionaryRef
无法强制转换为
Dictionary
@Darrarski您可以强制转换为Dictionary吗?(注意,需要将其转换为可选项)。我已经更新了一个快速示例。(注:最好是在新版本中不再有效的东西上发表评论,而不是否决投票)。@SoOverIt看起来事情又变了——在XCode 7.3中,我不得不使用
NSDictionary
import UIKit
import ImageIO

class 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, 0, nil) as Dictionary? {
                    let pixelWidth = imageProperties[kCGImagePropertyPixelWidth] as! Int
                    print("the image width is: \(pixelWidth)")
                }
            }
        }
    }
}