美文网首页iOS技术专题
iOS 图片高斯模糊的使用

iOS 图片高斯模糊的使用

作者: 隔墙送来秋千影 | 来源:发表于2018-05-17 13:44 被阅读14次

    方法一

    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 图片高斯模糊的使用

        本文链接:https://www.haomeiwen.com/subject/sksbdftx.html