0

I would like to apply the dark blur effect in a imageView for a Mac App, like you do it in iOS with the UIBlurEffect, how you do it in Cocoa OS X? What I want to achieve is the equivalent in OS X of this iOS Code:

var blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
var blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
backgroundImageView.addSubview(blurEffectView)

(This example is from "Beginning iOS 8 Programming with Swift" by Simon Ng)

shreyasm-dev
  • 2,711
  • 5
  • 16
  • 34
Cue
  • 2,952
  • 3
  • 33
  • 54
  • 2
    It sounds like what you are looking for is `NSVisualEffectView`, and there's a few answers that might work for you in this related question](http://stackoverflow.com/questions/24023183/how-can-i-create-yosemite-style-view-with-translucent-blurry-background). You'd just need to translate it to Swift. – Michael Dautermann Feb 28 '16 at 12:34
  • Thank you Michael. I rewrote a bit my question. – Cue Feb 28 '16 at 17:11

1 Answers1

0

You can use this code snippet to achieve a blur effect for an NSImage:

 func blur(image:NSImage, blurRadius:Double) -> NSImage {
    var originalImage = image
    var inputImage = CIImage(data: originalImage.tiffRepresentation!)
    var filter = CIFilter(name: "CIGaussianBlur")!
    filter.setDefaults()
    filter.setValue(inputImage, forKey: kCIInputImageKey)
    filter.setValue(10, forKey: kCIInputRadiusKey) // values from 0-20
    var outputImage = filter.value(forKey: kCIOutputImageKey) as! CIImage
    var outputImageRect = NSRectFromCGRect(outputImage.extent)
    var blurredImage = NSImage(size: outputImageRect.size)
    blurredImage.lockFocus()
    outputImage.draw(at: NSZeroPoint, from: outputImageRect, operation: .copy, fraction: 1.0)
    blurredImage.unlockFocus()
    
    return blurredImage
}

Or, you can add a content filter via IB:

enter image description here

C-Viorel
  • 1,801
  • 3
  • 22
  • 41