快速读取CFDictionary中的值

编程入门 行业动态 更新时间:2024-10-23 01:41:38
本文介绍了快速读取CFDictionary中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我只是从迅捷和可可开始.我正在尝试创建一个进行图像处理的基本应用.

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()

该词典包含以下信息:

{ 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"存在一些问题.

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.

let dpiH:NSNumber = CFDictionaryGetValue(imageDict, kCGImagePropertyDPIWidth)

我在做错什么,如何获得所需的字典值?

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

推荐答案

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

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

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

Swift 2.0的快速更新(对所有if let都不好意思-我刚刚编写了这段代码):

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

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

更多推荐

快速读取CFDictionary中的值

本文发布于:2023-11-11 06:16:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577603.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:快速   CFDictionary

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!