我只是围绕
Swift – 然后来了
Swift 1.2(打破我的工作代码)!
我有一个基于NSHipster – CGImageSourceCreateThumbnailAtIndex的代码示例的函数.
我以前工作的代码是:
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,options)) // do other stuff } } } }
从Swift 1.2开始,编译器提供了与选项字典相关的两个错误:
>表达式的类型不明确,没有更多的上下文
>’_’不能转换为’BooleanLiteralConvertible'(在ref为’true’值时)
我已经尝试了多种方法来明确声明选项字典中的类型(例如[String:Any],[CFString:Any],[Any:Any]).虽然这可以解决一个错误,但它们会引入其他错误.
任何人都可以照亮我吗?
更重要的是,任何人都可以解释用Swift 1.2和字典改变的内容,这些都阻止了这种情况的发生.
解决方法
从Xcode 6.3发行说明:
The implicit conversions from bridged Objective-C classes
(Nsstring/NSArray/NSDictionary) to their corresponding Swift value
types (String/Array/Dictionary) have been removed,making the Swift
type system simpler and more predictable.
您的案例中的问题是像KCGImageSourceThumbnailMaxPixelSize这样的CFStrings.这些不是自动的
转换为String了.两种可能的解决方
let options = [ kCGImageSourceThumbnailMaxPixelSize as String : maxSize,kCGImageSourceCreateThumbnailFromImageIfAbsent as String : true ]
要么
let options : [Nsstring : AnyObject ] = [ kCGImageSourceThumbnailMaxPixelSize: maxSize,kCGImageSourceCreateThumbnailFromImageIfAbsent: true ]