Created
August 10, 2018 09:00
-
-
Save Viveron/c0edc24198f4fad7bc4ee5802fe01fe0 to your computer and use it in GitHub Desktop.
UIImage box blur creation category
This file contains 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
#import <UIKit/UIKit.h> | |
@interface UIImage (Blur) | |
- (UIImage *)boxBlur:(CGFloat)blurLevel depth:(NSUInteger)blurDepth; | |
@end |
This file contains 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
#import "UIImage+Blur.h" | |
#import <float.h> | |
@import Accelerate; | |
@implementation UIImage (Blur) | |
- (UIImage *)boxBlur:(CGFloat)blurLevel depth:(NSUInteger)blurDepth { | |
blurLevel = MIN(1.0, MAX(0.0, blurLevel)); | |
blurDepth = MAX(1, MIN(100, blurDepth)); | |
int boxSize = (int)(blurLevel * 0.1 * MIN(self.size.width, self.size.height)); | |
boxSize = boxSize - (boxSize % 2) + 1; | |
NSData *imageData = UIImagePNGRepresentation(self); | |
UIImage *tmpImage = [UIImage imageWithData:imageData]; | |
CGImageRef img = tmpImage.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)); | |
outBuffer.data = pixelBuffer; | |
outBuffer.width = CGImageGetWidth(img); | |
outBuffer.height = CGImageGetHeight(img); | |
outBuffer.rowBytes = CGImageGetBytesPerRow(img); | |
for (NSUInteger index = 0; index < blurDepth; index++) { | |
error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend)?: | |
vImageBoxConvolve_ARGB8888(&outBuffer, &inBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend); | |
if (error) { | |
break; | |
} | |
} | |
outBuffer = inBuffer; | |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | |
CGContextRef ctx = CGBitmapContextCreate(outBuffer.data, | |
outBuffer.width, | |
outBuffer.height, | |
8, | |
outBuffer.rowBytes, | |
colorSpace, | |
kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast); | |
CGImageRef imageRef = CGBitmapContextCreateImage(ctx); | |
UIImage *returnImage = [UIImage imageWithCGImage:imageRef]; | |
CGContextRelease(ctx); | |
CGColorSpaceRelease(colorSpace); | |
free(pixelBuffer); | |
CFRelease(inBitmapData); | |
CGImageRelease(imageRef); | |
return returnImage; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment