Подтвердить что ты не робот

Значение чтения в CFDictionary с быстрым

Я только начинаю с быстрых и cocoa. Я пытаюсь создать базовое приложение, которое обрабатывает изображения.

Я уже получил всю информацию об этом:

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 со следующим кодом, и есть некоторая проблема с "__conversion", которую я не понимаю.

let dpiH:NSNumber = CFDictionaryGetValue(imageDict, kCGImagePropertyDPIWidth)

что я делаю неправильно и как я могу получить нужные значения словаря?

4b9b3361

Ответ 1

Мне было гораздо легче получить доступ к свойствам, "преобразовывая" 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 - я просто быстро создал этот код):

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