主要讲使用vImage来实现图片模糊效果
- 1.模糊效果代码(两套)
- 2.模糊效果发红问题(发红问题是因为图片原因)
-
3.先看看效果,为什么是两套代码(我这个GIF截图工具太垃圾了)
gs.gif
使用之前要导入#import <Accelerate/Accelerate.h>
这个框架,但是有个问题就是,代码一和代码二在我两个项目中刚好相反,一个发红一个不发红,我同样代码为什么会出现这样的情况,经过我的分析,处理,得出结果,是因为,我一个图片是直接用原图片,一个是用截屏,或者OpenGL截屏上下文得到的图片,应该是内部图片丢了写东西造成的
代码一(View截图生成的图片)
/**
高斯模糊(对用content截图,opengl截图的图片发红处理高斯模糊)
@param image 图片
@param blur 1-100(最好是1-25)
@return 高斯模糊图片
*/
+(UIImage *)fan_accelerateBlurWithImage:(UIImage *)image blurNumber:(CGFloat)blur
{
if(image==nil){
return nil;
}
int boxSize = blur;
if (blur<1||blur>100) {
boxSize=25;
}
boxSize = boxSize - (boxSize % 2) + 1;
CGImageRef img = image.CGImage;
vImage_Buffer inBuffer, outBuffer, rgbOutBuffer;
vImage_Error error;
void *pixelBuffer, *convertBuffer;
CGDataProviderRef inProvider = CGImageGetDataProvider(img);
CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
convertBuffer = malloc( CGImageGetBytesPerRow(img) * CGImageGetHeight(img) );
rgbOutBuffer.width = CGImageGetWidth(img);
rgbOutBuffer.height = CGImageGetHeight(img);
rgbOutBuffer.rowBytes = CGImageGetBytesPerRow(img);
rgbOutBuffer.data = convertBuffer;
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);
void *rgbConvertBuffer = malloc( CGImageGetBytesPerRow(img) * CGImageGetHeight(img) );
vImage_Buffer outRGBBuffer;
outRGBBuffer.width = CGImageGetWidth(img);
outRGBBuffer.height = CGImageGetHeight(img);
outRGBBuffer.rowBytes = CGImageGetBytesPerRow(img);//3
outRGBBuffer.data = rgbConvertBuffer;
error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
// error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
if (error) {
NSLog(@"error from convolution %ld", error);
}
const uint8_t mask[] = {2, 1, 0, 3};
vImagePermuteChannels_ARGB8888(&outBuffer, &rgbOutBuffer, mask, kvImageNoFlags);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(rgbOutBuffer.data,
rgbOutBuffer.width,
rgbOutBuffer.height,
8,
rgbOutBuffer.rowBytes,
colorSpace,
kCGImageAlphaNoneSkipLast);
CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
//clean up
CGContextRelease(ctx);
free(pixelBuffer);
free(convertBuffer);
free(rgbConvertBuffer);
CFRelease(inBitmapData);
CGColorSpaceRelease(colorSpace);
CGImageRelease(imageRef);
return returnImage;
}
代码二 对原图片处理
/**
高斯模糊(直接对原图片高斯模糊)
@param image 图片
@param blur 1-100(最好是1-25)
@return 高斯模糊图片
*/
+(UIImage *)fan_accelerateBlurShortWithImage:(UIImage *)image blurNumber:(CGFloat)blur
{
if(image==nil){
return nil;
}
int boxSize = blur;
if (blur<1||blur>100) {
boxSize=25;
}
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;
}
下面看看我的截屏代码,这两套截屏代码都必须用代码一来高斯模糊不然会发红
/** 截屏-不能截图播放器View,或者Unity的OpenGLView*/
+(UIImage*)fan_beginImageContext:(CGRect)rect fromView:(UIView*)view
{
UIGraphicsBeginImageContext(view.frame.size); //currentView 当前的view
//取得当前画布的上下文UIGraphicsGetCurrentContext render渲染
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//从全屏中截取指定的范围
CGImageRef imageRef = viewImage.CGImage;
CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect];
/******截取图片保存的位置,如果想要保存,请把return向后移动*********/
// CGImageRelease(imageRef);//加入这个会崩溃,不知道为什么
CGImageRelease(imageRefRect);
return sendImage;
}
/** 截屏-能截图,播放器View,或者Unity的OpenGLView,也能截图普通的View*/
+ (UIImage *)fan_openglSnapshotImage:(UIView *)openGLView{
//图片位图的大小
CGSize size = openGLView.frame.size;
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
//View 内的图像放到size位图的位置
CGRect rect = openGLView.bounds;
// 自iOS7开始它允许你截取一个UIView或者其子类中的内容,并且以位图的形式(bitmap)保存到UIImage中
[openGLView drawViewHierarchyInRect:rect afterScreenUpdates:YES];
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return snapshotImage;
//AVCaptureVideoPreviewLayer 和 AVSampleBufferDisplayLayer可以用这个获取一个View,但是能添加,不能再截图
// UIView *snapView = [self snapshotViewAfterScreenUpdates:YES];
}
网友评论