-
-
Save billgarrison/5233971 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Ref: <http://indieambitions.com/idevblogaday/perform-blur-vimage-accelerate-framework-tutorial/> */ | |
- (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur { | |
if (blur < 0.f || blur > 1.f) { | |
blur = 0.5f; | |
} | |
int boxSize = (int)(blur * 100); | |
boxSize = boxSize - (boxSize % 2) + 1; | |
CGImageRef img = image.CGImage; | |
vImage_Buffer inBuffer, outBuffer; | |
vImage_Error error; | |
void *pixelBuffer; | |
CGDataProviderRef inProvider = CGImageGetDataProvider(img); | |
CFDataRef inBitmapData = CGDataProviderCopyData(inProvider); | |
inBuffer.width = CGImageGetWidth(img); | |
inBuffer.height = CGImageGetHeight(img); | |
inBuffer.rowBytes = CGImageGetBytesPerRow(img); | |
inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData); | |
pixelBuffer = malloc(CGImageGetBytesPerRow(img) * | |
CGImageGetHeight(img)); | |
if(pixelBuffer == NULL) | |
NSLog(@"No pixelbuffer"); | |
outBuffer.data = pixelBuffer; | |
outBuffer.width = CGImageGetWidth(img); | |
outBuffer.height = CGImageGetHeight(img); | |
outBuffer.rowBytes = CGImageGetBytesPerRow(img); | |
error = vImageBoxConvolve_ARGB8888(&inBuffer, | |
&outBuffer, | |
NULL, | |
0, | |
0, | |
boxSize, | |
boxSize, | |
NULL, | |
kvImageEdgeExtend); | |
if (error) { | |
NSLog(@"error from convolution %ld", error); | |
} | |
CGColorSpaceRef colorSpace = CGImageGetColorSpace(img); | |
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(img); | |
CGContextRef ctx = CGBitmapContextCreate( | |
outBuffer.data, | |
outBuffer.width, | |
outBuffer.height, | |
8, | |
outBuffer.rowBytes, | |
colorSpace, | |
bitmapInfo); | |
CGImageRef imageRef = CGBitmapContextCreateImage (ctx); | |
UIImage *returnImage = [UIImage imageWithCGImage:imageRef | |
scale:[[UIScreen mainScreen] scale] | |
orientation:UIImageOrientationUp]; | |
//clean up | |
CGContextRelease(ctx); | |
CGColorSpaceRelease(colorSpace); | |
free(pixelBuffer); | |
CFRelease(inBitmapData); | |
CGImageRelease(imageRef); | |
return returnImage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment