Skip to content

Instantly share code, notes, and snippets.

@fmtonakai
Created November 26, 2012 02:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fmtonakai/4146380 to your computer and use it in GitHub Desktop.
Save fmtonakai/4146380 to your computer and use it in GitHub Desktop.
UIImage Blur
//
// UIImage+Blur.h
// HTTPLoaderTest
//
// Created by masaki.fuke on 12/08/14.
// Copyright (c) 2012年 masaki.fuke. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIImage (Blur)
-(UIImage *)blurImageWithPoint:(NSUInteger)point;
-(UIImage *)gaussianBlurImage;
@end
//
// UIImage+Blur.m
// HTTPLoaderTest
//
// Created by masaki.fuke on 12/08/14.
// Copyright (c) 2012年 masaki.fuke. All rights reserved.
//
#import "UIImage+Blur.h"
#import <Accelerate/Accelerate.h>
static int16_t gaussianblur_kernel[13] = {
1, 12, 66, 220, 495, 792, 924, 792, 495, 220, 66, 12, 1
};
@implementation UIImage (Blur)
-(UIImage *)blurImageWithPoint:(NSUInteger)point
{
NSInteger index = point * 2 * self.scale + 1;
int16_t kernel[index];
for (NSInteger i = 0; i < index; i++) {
kernel[i] = 1;
}
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self drawAtPoint:CGPointZero];
size_t width = CGBitmapContextGetWidth(ctx);
size_t height = CGBitmapContextGetHeight(ctx);
size_t bytesPerRow = CGBitmapContextGetBytesPerRow(ctx);
UInt8 *data = CGBitmapContextGetData(ctx);
vImage_Buffer src = {data, height, width, bytesPerRow};
vImageConvolve_ARGB8888(&src, &src, NULL, 0, 0, kernel , index, 1, index, NULL, kvImageEdgeExtend);
vImageConvolve_ARGB8888(&src, &src, NULL, 0, 0, kernel, 1, index, index, NULL, kvImageEdgeExtend);
UIImage *blurImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return blurImage;
}
-(UIImage *)gaussianBlurImage
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self drawAtPoint:CGPointZero];
size_t width = CGBitmapContextGetWidth(ctx);
size_t height = CGBitmapContextGetHeight(ctx);
size_t bitsPerComponent = CGBitmapContextGetBitsPerComponent(ctx);
size_t bitsPerPixcel = CGBitmapContextGetBitsPerPixel(ctx);
size_t bytesPerRow = CGBitmapContextGetBytesPerRow(ctx);
NSLog(@"%ld, %ld, %ld, %ld, %ld", width, height, bitsPerComponent, bitsPerPixcel, bytesPerRow);
UInt8 *data = CGBitmapContextGetData(ctx);
vImage_Buffer src = {data, height, width, bytesPerRow};
vImageConvolve_ARGB8888(&src, &src, NULL, 0, 0, gaussianblur_kernel, 13, 1, 4096, NULL, kvImageEdgeExtend);
vImageConvolve_ARGB8888(&src, &src, NULL, 0, 0, gaussianblur_kernel, 1, 13, 4096, NULL, kvImageEdgeExtend);
UIImage *blurImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return blurImage;
}
@end
//
// UIImage+Blur.h
// HTTPLoaderTest
//
// Created by masaki.fuke on 12/08/14.
// Copyright (c) 2012年 masaki.fuke. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIImage (Blur)
-(UIImage *)blurImageWithPoint:(NSUInteger)point;
-(UIImage *)gaussianBlurImage;
@end
//
// UIImage+Blur.m
// HTTPLoaderTest
//
// Created by masaki.fuke on 12/08/14.
// Copyright (c) 2012年 masaki.fuke. All rights reserved.
//
#import "UIImage+Blur.h"
#import <Accelerate/Accelerate.h>
static int16_t gaussianblur_kernel[13] = {
1, 12, 66, 220, 495, 792, 924, 792, 495, 220, 66, 12, 1
};
@implementation UIImage (Blur)
-(UIImage *)blurImageWithPoint:(NSUInteger)point
{
NSInteger index = point * 2 * self.scale + 1;
int16_t kernel[index];
for (NSInteger i = 0; i < index; i++) {
kernel[i] = 1;
}
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self drawAtPoint:CGPointZero];
size_t width = CGBitmapContextGetWidth(ctx);
size_t height = CGBitmapContextGetHeight(ctx);
size_t bytesPerRow = CGBitmapContextGetBytesPerRow(ctx);
UInt8 *data = CGBitmapContextGetData(ctx);
vImage_Buffer src = {data, height, width, bytesPerRow};
vImageConvolve_ARGB8888(&src, &src, NULL, 0, 0, kernel , index, 1, index, NULL, kvImageEdgeExtend);
vImageConvolve_ARGB8888(&src, &src, NULL, 0, 0, kernel, 1, index, index, NULL, kvImageEdgeExtend);
UIImage *blurImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return blurImage;
}
-(UIImage *)gaussianBlurImage
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self drawAtPoint:CGPointZero];
size_t width = CGBitmapContextGetWidth(ctx);
size_t height = CGBitmapContextGetHeight(ctx);
size_t bitsPerComponent = CGBitmapContextGetBitsPerComponent(ctx);
size_t bitsPerPixcel = CGBitmapContextGetBitsPerPixel(ctx);
size_t bytesPerRow = CGBitmapContextGetBytesPerRow(ctx);
UInt8 *data = CGBitmapContextGetData(ctx);
vImage_Buffer src = {data, height, width, bytesPerRow};
vImageConvolve_ARGB8888(&src, &src, NULL, 0, 0, gaussianblur_kernel, 13, 1, 4096, NULL, kvImageEdgeExtend);
vImageConvolve_ARGB8888(&src, &src, NULL, 0, 0, gaussianblur_kernel, 1, 13, 4096, NULL, kvImageEdgeExtend);
UIImage *blurImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return blurImage;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment