Created
January 4, 2014 11:34
-
-
Save yaozhuoyu/8254467 to your computer and use it in GitHub Desktop.
image helper from https://github.com/NZN/UIImage-Helpers
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
- (UIImage*)blurredImage:(CGFloat)blurAmount | |
{ | |
if (blurAmount < 0.0 || blurAmount > 1.0) { | |
blurAmount = 0.5; | |
} | |
int boxSize = (int)(blurAmount * 40); | |
boxSize = boxSize - (boxSize % 2) + 1; | |
CGImageRef img = self.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); | |
error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend); | |
if (!error) { | |
error = vImageBoxConvolve_ARGB8888(&outBuffer, &inBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend); | |
if (!error) { | |
error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend); | |
} | |
} | |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | |
CGContextRef ctx = CGBitmapContextCreate(outBuffer.data, | |
outBuffer.width, | |
outBuffer.height, | |
8, | |
outBuffer.rowBytes, | |
colorSpace, | |
(CGBitmapInfo)kCGImageAlphaNoneSkipLast); | |
CGImageRef imageRef = CGBitmapContextCreateImage (ctx); | |
UIImage *returnImage = [UIImage imageWithCGImage:imageRef]; | |
CGContextRelease(ctx); | |
CGColorSpaceRelease(colorSpace); | |
free(pixelBuffer); | |
CFRelease(inBitmapData); | |
CGColorSpaceRelease(colorSpace); | |
CGImageRelease(imageRef); | |
return returnImage; | |
} | |
+ (UIImage *)imageWithColor:(UIColor *)color | |
{ | |
CGRect rect = CGRectMake(0, 0, 1, 1); | |
UIGraphicsBeginImageContext(rect.size); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGContextSetFillColorWithColor(context, [color CGColor]); | |
CGContextFillRect(context, rect); | |
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return image; | |
} | |
+ (UIImage *)screenshot | |
{ | |
CGSize imageSize = [[UIScreen mainScreen] bounds].size; | |
if (NULL != UIGraphicsBeginImageContextWithOptions) { | |
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); | |
} else { | |
UIGraphicsBeginImageContext(imageSize); | |
} | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
for (UIWindow *window in [[UIApplication sharedApplication] windows]) { | |
if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) { | |
CGContextSaveGState(context); | |
CGContextTranslateCTM(context, [window center].x, [window center].y); | |
CGContextConcatCTM(context, [window transform]); | |
CGContextTranslateCTM(context, | |
-[window bounds].size.width * [[window layer] anchorPoint].x, | |
-[window bounds].size.height * [[window layer] anchorPoint].y); | |
[[window layer] renderInContext:context]; | |
CGContextRestoreGState(context); | |
} | |
} | |
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return image; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment