本文主要是介绍高斯模糊 毛玻璃 可以调节模糊度,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、框架
Accelerate.framework。
2、头文件
#import <Accelerate/Accelerate.h>
// 高斯模糊
-(UIImage*)getBlurImage:(UIImage*)image
{
return [selfgaussBlur:0.2andImage:image];
}
- (UIImage*)gaussBlur:(CGFloat)blurLevel andImage:(UIImage*)originImage
{
blurLevel = MIN(1.0,MAX(0.0, blurLevel));
//int boxSize = (int)(blurLevel * 0.1 * MIN(self.size.width, self.size.height));
int boxSize = 20;//模糊度参数配置,可以根据需要修改
boxSize = boxSize - (boxSize % 2) + 1;
NSData *imageData = UIImageJPEGRepresentation(originImage, 1);
UIImage *tmpImage = [UIImageimageWithData:imageData];
CGImageRef img = tmpImage.CGImage;
vImage_Buffer inBuffer, outBuffer;
vImage_Error error;
void *pixelBuffer;
//create vImage_Buffer with data from CGImageRef
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);
//create vImage_Buffer for output
pixelBuffer = malloc(CGImageGetBytesPerRow(img) *CGImageGetHeight(img));
outBuffer.data = pixelBuffer;
outBuffer.width = CGImageGetWidth(img);
outBuffer.height = CGImageGetHeight(img);
outBuffer.rowBytes = CGImageGetBytesPerRow(img);
NSInteger windowR = boxSize/2;
CGFloat sig2 = windowR / 3.0;
if(windowR>0){ sig2 = -1/(2*sig2*sig2); }
int16_t *kernel = (int16_t*)malloc(boxSize*sizeof(int16_t));
int32_t sum = 0;
for(NSInteger i=0; i<boxSize; ++i){
kernel[i] = 255*exp(sig2*(i-windowR)*(i-windowR));
sum += kernel[i];
}
free(kernel);
// convolution
error = vImageConvolve_ARGB8888(&inBuffer, &outBuffer,NULL,0, 0, kernel, boxSize,1, sum, NULL,kvImageEdgeExtend);
error = vImageConvolve_ARGB8888(&outBuffer, &inBuffer,NULL,0, 0, kernel,1, boxSize, sum, NULL,kvImageEdgeExtend);
outBuffer = inBuffer;
if (error) {
//NSLog(@"error from convolution %ld", error);
}
CGColorSpaceRef colorSpace =CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(outBuffer.data,
outBuffer.width,
outBuffer.height,
8,
outBuffer.rowBytes,
colorSpace,
kCGBitmapAlphaInfoMask &kCGImageAlphaNoneSkipLast);
CGImageRef imageRef =CGBitmapContextCreateImage(ctx);
UIImage *returnImage = [UIImageimageWithCGImage:imageRef];
//clean up
CGContextRelease(ctx);
CGColorSpaceRelease(colorSpace);
free(pixelBuffer);
CFRelease(inBitmapData);
CGImageRelease(imageRef);
return returnImage;
}
这篇关于高斯模糊 毛玻璃 可以调节模糊度的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!