需求起因
某个页面需要将一个尚未生成的页面进行高斯模糊作为背景。
关于高斯模糊效果实现方案选取:
参考资料请移步这里
最后选取Accelerate的方法,生成高斯模糊图片耗时约0.05s
具体实现
//创建vc
SecondViewController *vc = [[SecondViewController alloc]init];
//后台执行:
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
NSLog(@"begin");
//截图
NSInteger width = vc.view.frame.size.width;
NSInteger heihgt = vc.view.frame.size.height;
CGSize size = CGSizeMake(width,heihgt);
UIGraphicsBeginImageContext(size);
[vc.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//通知主线程刷新
dispatch_async(dispatch_get_main_queue(), ^{
self.imgView.image = [self blurryImage:image withBlurLevel:0.05f];
});
NSLog(@"end");
});
//毛玻璃效果。
- (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 = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(
outBuffer.data,
outBuffer.width,
outBuffer.height,
8,
outBuffer.rowBytes,
colorSpace,
kCGImageAlphaNoneSkipLast);
CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
//clean up
CGContextRelease(ctx);
CGColorSpaceRelease(colorSpace);
free(pixelBuffer);
CFRelease(inBitmapData);
CGColorSpaceRelease(colorSpace);
CGImageRelease(imageRef);
return returnImage;
}
详见demo:GaussianBlurDemo
更多
其实考虑到异步加载cell内容、数据刷新后背景图的更新,相比于我给出的这个小demo,实现还要复杂一些。
下面把一些思路贴出来,就不上代码了:
1.建议使用单例统一处理,回调采用不然就是通知漫天飞的情况,不利于以后的更改和管理。
2.cell全部加载完成后,再显示背景图,可以创建一个数组,放入所有待绘制图片数据的唯一标识(建议使用url),使用SDWebImage,在绘制完成后,将数组中的唯一标识移除,当数组长度为0时,表示全部加载完毕,这样,就可以调用单例来处理高斯模糊,并通知更换背景图。
简书已经弃用,欢迎移步我的小专栏:
https://xiaozhuanlan.com/dahuihuiiOS