我刚刚开始了解 Swift - 然后出现了 Swift 1.2(破坏了我的工作代码)!
我有一个基于代码示例的函数NSHipster - CGImageSourceCreateThumbnailAtIndex http://nshipster.com/image-resizing/.
我以前的工作代码是:
import ImageIO
func processImage(jpgImagePath: String, thumbSize: CGSize) {
if let path = NSBundle.mainBundle().pathForResource(jpgImagePath, ofType: "") {
if let imageURL = NSURL(fileURLWithPath: path) {
if let imageSource = CGImageSourceCreateWithURL(imageURL, nil) {
let maxSize = max(thumbSize.width, thumbSize.height) / 2.0
let options = [
kCGImageSourceThumbnailMaxPixelSize: maxSize,
kCGImageSourceCreateThumbnailFromImageIfAbsent: true
]
let scaledImage = UIImage(CGImage: CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options))
// do other stuff
}
}
}
}
从 Swift 1.2 开始,编译器提供了两个与options
字典:
- 表达类型不明确,没有更多上下文
-
“_”无法转换为“BooleanLiteralConvertible”(参考“真实”值)
我尝试了多种方法来专门声明选项字典中的类型(例如[String : Any]
, [CFString : Any]
, [Any : Any]
)。虽然这可以解决一个错误,但它们会引入其他错误。
谁能照亮我吗?
更重要的是,任何人都可以解释一下 Swift 1.2 和字典中发生了什么变化导致它无法工作。
来自 Xcode 6.3 发行说明:
来自桥接 Objective-C 类的隐式转换
(NSString/NSArray/NSDictionary) 为其对应的 Swift 值
类型(字符串/数组/字典)已被删除,使得 Swift
类型系统更简单、更可预测。
你的情况的问题是CFString
s like kCGImageSourceThumbnailMaxPixelSize
。这些不是自动的
转换成String
不再了。两种可能的解决方案:
let options = [
kCGImageSourceThumbnailMaxPixelSize as String : maxSize,
kCGImageSourceCreateThumbnailFromImageIfAbsent as String : true
]
or
let options : [NSString : AnyObject ] = [
kCGImageSourceThumbnailMaxPixelSize: maxSize,
kCGImageSourceCreateThumbnailFromImageIfAbsent: true
]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)