CIFilter和Swift 2的错误:'_'不能转换为'String'+没有更多上下文的表达式是不明确的

时间:2015-07-07 20:30:51

标签: swift cocoa core-image nsimage swift2

我在Swift 1.2中使用了这段代码(改编自SO上的Objective-C片段),以对图像应用模糊效果。

它工作正常,但我无法将其转换为Swift 2,我不确定我是否理解错误消息,而且找不到有关更改内容的文档。

错误讯息:

  

'_'不能转换为'String'

  

如果没有更多的上下文,表达的类型是不明确的

与1.2配合使用的扩展:

extension NSImage {
    func gaussianBlurOfRadius(radius: CGFloat) -> NSImage {
        let image = self
        image.lockFocus()
        let beginImage = CIImage(data: image.TIFFRepresentation!)
        // error message highlights the next line
        let params = [kCIInputImageKey: beginImage, kCIInputRadiusKey: radius]
        let filter = CIFilter(name: "CIGaussianBlur", withInputParameters: params)
        var output = filter.valueForKey("outputImage") as! CIImage
        let rect = NSMakeRect(0, 0, self.size.width, self.size.height)
        output.drawInRect(rect, fromRect: rect, operation: NSCompositingOperation.CompositeSourceOver, fraction: 1)
        image.unlockFocus()
        return image
    }
}

enter image description here

更新:

根据Duncan的想法,我在声明中添加了一个显式类型:

let params: [String: AnyObject] = [kCIInputImageKey: beginImage, kCIInputRadiusKey: radius]

enter image description here

但这并没有解决它,但它删除了有关类型歧义的错误消息。

2 个答案:

答案 0 :(得分:1)

extension NSImage {
    func gaussianBlurOfRadius(radius: CGFloat) -> NSImage {
        let image = self
        image.lockFocus()
        let beginImage = CIImage(data: image.TIFFRepresentation!)!
        let params = [kCIInputImageKey : beginImage, kCIInputRadiusKey: radius]
        let filter = CIFilter(name: "CIGaussianBlur", withInputParameters: params)!
        let output = filter.valueForKey("outputImage") as! CIImage
        let rect = NSMakeRect(0, 0, size.width, size.height)
        output.drawInRect(rect, fromRect: rect, operation: NSCompositingOperation.CompositeSourceOver, fraction: 1)
        image.unlockFocus()
        return image
    }
}

extension NSImage {
    func gaussianBlurOfRadius(radius: CGFloat) -> NSImage {
        let image = self
        image.lockFocus()
        let beginImage = CIImage(data: image.TIFFRepresentation!)
        let filter = CIFilter(name: "CIGaussianBlur")!
        filter.setValue(beginImage, forKey: kCIInputImageKey)
        filter.setValue(radius, forKey: kCIInputRadiusKey)
        let output = filter.valueForKey("outputImage") as! CIImage
        let rect = NSMakeRect(0, 0, size.width, size.height)
        output.drawInRect(rect, fromRect: rect, operation: NSCompositingOperation.CompositeSourceOver, fraction: 1)
        image.unlockFocus()
        return image
    }
}

答案 1 :(得分:0)

猜测,Swift正在尝试创建一个固定类型的Dictionary对象。

尝试

let params: [String: AnyObject] = 
  [kCIInputImageKey: beginImage, kCIInputRadiusKey: radius]

或者

let params: NSDictionary = 
  [kCIInputImageKey: beginImage, kCIInputRadiusKey: radius]