Eric Aya
Eric Aya

Reputation: 70113

Errors with CIFilter and Swift 2: '_' is not convertible to 'String' + Type of expression is ambiguous without more context

I used this piece of code in Swift 1.2 (adapted from an Objective-C snippet found on SO) to apply a blur effect on an image.

It was working ok but I can't convert it to Swift 2, I'm not sure I understand the error messages and I can't find documentation about what has changed.

Error message:

'_' is not convertible to 'String'

And:

Type of expression is ambiguous without more context

Extension that worked with 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

UPDATE:

Following Duncan's idea, I've added an explicit type to the declaration:

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

enter image description here

but that doesn't fix it, though it removes the error message about type ambiguity.

Upvotes: 0

Views: 359

Answers (2)

Leo Dabus
Leo Dabus

Reputation: 236458

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

or

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

Upvotes: 1

Duncan C
Duncan C

Reputation: 131471

At a guess, Swift is trying to create a Dictionary object with fixed types.

try

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

Or perhaps

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

Upvotes: 0

Related Questions