Ответ 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)")
}
}
}
}
}