Skip to content

Instantly share code, notes, and snippets.

@TomLiu
Last active March 11, 2016 06:40
Show Gist options
  • Save TomLiu/7635912 to your computer and use it in GitHub Desktop.
Save TomLiu/7635912 to your computer and use it in GitHub Desktop.
return a GaussianBlured NSImage
//
// NSImage+Blur.h
// BlurView
//
// Created by 61 on 13-11-19.
// Copyright (c) 2013年 61. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface NSImage (Blur)
- (NSImage *)gaussianBlurOfRadius:(CGFloat)radius;
@end
//
// NSImage+Blur.m
// BlurView
//
// Created by 61 on 13-11-19.
// Copyright (c) 2013年 61. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "NSImage+Blur.h"
@implementation NSImage (Blur)
- (NSImage *)gaussianBlurOfRadius:(CGFloat)radius
{
NSScreen *mainScreen = [NSScreen mainScreen];
NSRect screenFrame = mainScreen.frame;
NSImage *image = [self scaleToSize:screenFrame.size];
[image lockFocus];
CIImage *beginImage = [[CIImage alloc] initWithData:[image TIFFRepresentation]];
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur" keysAndValues:kCIInputImageKey, beginImage, @"inputRadius", @(radius), nil];
CIImage *output = [filter valueForKey:@"outputImage"];
NSRect rect = NSMakeRect(0, 0, NSWidth(screenFrame), NSHeight(screenFrame));
NSRect sourceRect = NSMakeRect(0, 0, self.size.width, self.size.height);
[output drawInRect:rect fromRect:sourceRect operation:NSCompositeSourceOver fraction:1.0];
[image unlockFocus];
return image;
}
@end
@nicolas-miari
Copy link

No visible @interface for 'NSImage' declares the selector 'scaleToSize:'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment