用swift读取CFDictionary中的值

时间:2021-06-17 21:26:10

I'm just starting with swift and cocoa. I'm trying to create a basic app that does image manipulation.

我只是从斯威夫特和可可开始。我想创建一个做图像处理的基本应用。

I've allready got all information of the image with this:

我已经准备好了图像的所有信息:

let imageRef:CGImageSourceRef = CGImageSourceCreateWithURL(url, nil).takeUnretainedValue()
let imageDict:CFDictionaryRef = CGImageSourceCopyPropertiesAtIndex(imageRef, 0, nil).takeUnretainedValue()

the dictionary contains following information:

本词典包含以下信息:

{
    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;
    };
}

now I'd like to read the value for the DPI with following code and there is some problem with "__conversion" I don't understand.

现在我想用下面的代码读取DPI的值,我不明白“__conversion”有一些问题。

let dpiH:NSNumber = CFDictionaryGetValue(imageDict, kCGImagePropertyDPIWidth)

what am I doing wrong and how can I get to the desired values of the dictionary?

我做错了什么?我怎样才能得到字典想要的值?

1 个解决方案

#1


12  

I've found it much easier to access the properties by 'converting' the CFDictionary to a Swift dictionary.

我发现通过将CFDictionary“转换”为Swift字典,更容易访问属性。

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

Quick update for Swift 2.0 (excuse all the if lets - I just quickly crafted this code):

Swift 2.0的快速更新(请原谅所有if let -我只是快速编写了这段代码):

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)")
                }
            }
        }
    }
}

#1


12  

I've found it much easier to access the properties by 'converting' the CFDictionary to a Swift dictionary.

我发现通过将CFDictionary“转换”为Swift字典,更容易访问属性。

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

Quick update for Swift 2.0 (excuse all the if lets - I just quickly crafted this code):

Swift 2.0的快速更新(请原谅所有if let -我只是快速编写了这段代码):

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)")
                }
            }
        }
    }
}