iOS 图片高斯模糊的使用

释放双眼,带上耳机,听听看~!

方法一

1,设置原图片

UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 140 *ZZHeight)];
[imgV sd_setImageWithURL:[NSURL URLWithString:_img_url] placeholderImage:[UIImage imageNamed:@"archImg"]];

2,设置高斯模糊

UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
effectView.frame = CGRectMake(0, 0, WIDTH, 150 *ZZHeight);

3,将高斯模糊效果添加到原图片上

[imgV addSubview:effectView];
[self.view addSubview:imgV];
//感觉并不是太好用  效果比较单一  并且模糊效果很重  完全看不出来有原图片的痕迹  
//如果要求不高  可以使用

方法二

//1,添加方法
+(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;
}

//2,调用方法
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 140 *ZZHeight)];
[imgV sd_setImageWithURL:[NSURL URLWithString:_img_url] placeholderImage:[UIImage imageNamed:@"archImg"]];
imgV.image = [CommonHelper coreBlurImage:imgV.image withBlurNumber:9];
[self.view addSubview:imgV];
//这个倒是比方法一好很多  可以自定义模糊值  从0到无穷大  但是到了十几二十以后就失去了模糊效果的初衷了  建议十以内   当然  看自己选择
//但是也有一个缺点是  加了效果后的图片会变小并且四周有白边

方法三

//常用的方法还有一个vImage
//但是此方法属于Accelerate.Framework,需要导入 Accelerate下的 Accelerate头文件。

#import <Accelerate/Accelerate.h>
+(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,
   outBuffer.rowBytes,
   colorSpace,
   kCGImageAlphaNoneSkipLast);
   CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
   UIImage *returnImage = [UIImage imageWithCGImage:imageRef];

   CGContextRelease(ctx);
   CGColorSpaceRelease(colorSpace);
   free(pixelBuffer);
   CFRelease(inBitmapData);
   CGColorSpaceRelease(colorSpace);
   CGImageRelease(imageRef);
   return returnImage;
}

//2,调用方法
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 140 *ZZHeight)];
[imgV sd_setImageWithURL:[NSURL URLWithString:_img_url] placeholderImage:[UIImage imageNamed:@"archImg"]];
imgV.image = [CommonHelper boxblurImage:imgV.image withBlurNumber:9];
[self.view addSubview:imgV];

建议使用第三种

 

人已赞赏
iOS文章

iOS 如何点击TextField不弹出键盘?

2019-10-14 11:58:47

iOS文章

iOS 转场动画详解

2019-10-14 12:34:14

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索