gpt4 book ai didi

macos - 使用 swift 读取 CFDictionary 中的值

转载 作者:行者123 更新时间:2023-11-28 08:32:16 25 4
gpt4 key购买 nike

我刚开始使用 swift 和 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)

我做错了什么,我怎样才能得到字典的期望值?

最佳答案

我发现通过将 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)")
}
}
}
}
}

关于macos - 使用 swift 读取 CFDictionary 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38638889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com