public
Last active

UIImage Blur

  • Download Gist
UIImage+Blur.h
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//
// 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
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
//
// 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
UIImageBlur.h
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//
// 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
UIImageBlur.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
//
// 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.