本文主要是介绍图片模糊效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#import "UIImage+Blur.h"
#import <Accelerate/Accelerate.h>
@implementation UIImage (Blur)
//图片模糊方法
+ (UIImage *)boxblurImage:(UIImage *)image withBlurNumber:(CGFloat)blur{
if (blur < 0.f||blur > 1.f) {
blur = 0.5f;
}
int boxSize = (int)(blur *40);
boxSize = boxSize - (boxSize %2)+1;
CGImageRef img = image.CGImage;
vImage_Buffer inBuffer,outBuffer;
vImage_Error error;
void *pixelBuffer;
//从CGImage中获取数据
CGDataProviderRef inProvider = CGImageGetDataProvider(img);
CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
//设置从CGImage获取对象属性
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;
}
//模糊图片方法
+ (UIImage *)coreBlurImage:(UIImage *)image withBlurNumber:(CGFloat)blur{
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *inputImage =[CIImage imageWithCGImage:image.CGImage];
CIFilter *filter =[CIFilter filterWithName:@"CIGaussianBlur"];
[filter setValue:inputImage forKey:kCIInputImageKey];
[filter setValue:@(blur) forKey:@"inputRadius"];
//模糊图片
CIImage *result =[filter valueForKey:kCIOutputImageKey];
CGImageRef outImage =[context createCGImage:result fromRect:[result extent]];
UIImage *blurImage =[UIImage imageWithCGImage:outImage];
CGImageRelease(outImage);
return blurImage;
}
这篇关于图片模糊效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!