4

I'm searching for a way to add a blur effect to a NSImage using Swift.

developing for iOS, UIImage provides a method like

applyLightEffectAtFrame:frame

... but i could not find something equal for Cocoa/an OSX-App.

edit 1: i tried to use CIFilter:

let beginImage = CIImage(data: responseData)
let filter = CIFilter(name: "CIGaussianBlur")
filter.setValue(beginImage, forKey: kCIInputImageKey)
filter.setValue(0.5, forKey: kCIInputIntensityKey)
// HOW CAN I MAKE AN NSIMAGE OUT OF THE CIIMAGE?
let newImage = ???
imageView.image = newImage
ixany
  • 5,433
  • 9
  • 41
  • 65
  • Core Image filters are the general equivalent to a lot of the image effects you can find on iOS. – CodaFi Oct 11 '14 at 00:35
  • edited my question above; i tried to use the CIFilter thinking i'm on a good way, but couldn't find out how i can convert the CIImage back to NSImage? – ixany Oct 11 '14 at 12:41
  • It's a simple enough process using image reps. See: http://stackoverflow.com/questions/17386650/converting-ciimage-into-nsimage – CodaFi Oct 11 '14 at 21:50

2 Answers2

2

I solved this issue by just adding a content filter to my NSImageView within the Interface-Builder.

ixany
  • 5,433
  • 9
  • 41
  • 65
2

The following (taken from here) works for me

    var originalImage = NSImage(named: "myImageName")
    var inputImage = CIImage(data: originalImage?.TIFFRepresentation)
    var filter = CIFilter(name: "CIGaussianBlur")
    filter.setDefaults()
    filter.setValue(inputImage, forKey: kCIInputImageKey)
    var outputImage = filter.valueForKey(kCIOutputImageKey) as CIImage
    var outputImageRect = NSRectFromCGRect(outputImage.extent())
    var blurredImage = NSImage(size: outputImageRect.size)
    blurredImage.lockFocus()
    outputImage.drawAtPoint(NSZeroPoint, fromRect: outputImageRect, operation: .CompositeCopy, fraction: 1.0)
    blurredImage.unlockFocus()

    imageView.image = blurredImage

Hope it helps the new comers

Q8i
  • 1,767
  • 17
  • 25