gpt4 book ai didi

swift - CIFilter 和 Swift 2 错误 : '_' is not convertible to 'String' + Type of expression is ambiguous without more context

转载 作者:行者123 更新时间:2023-11-30 10:14:00 32 4
gpt4 key购买 nike

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

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

错误消息:

'_' is not convertible to 'String'

还有:

Type of expression is ambiguous without more context

适用于 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

但这并不能解决问题,尽管它删除了有关类型歧义的错误消息。

最佳答案

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

关于swift - CIFilter 和 Swift 2 错误 : '_' is not convertible to 'String' + Type of expression is ambiguous without more context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31278679/

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