如何给毛玻璃设置它的模糊程度blur

效果如下:透明度是0.54 blur:2 saturation:0

如何给毛玻璃设置它的模糊程度blur_第1张图片


下面这种方法只有固定的类型可以选择 无法和UI保持一致

/*

 毛玻璃的样式(枚举)

 */

UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:self.view.bounds];

bgImgView.image = [UIImage imageNamed:@"huoying.jpg"];

[self.view addSubview:bgImgView];


UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];

UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];

effectView.frame = CGRectMake(0, 0, bgImgView.frame.size.width * 0.5, bgImgView.frame.size.height);

[bgImgView addSubview:effectView];


解决办法:

第一步:获取屏幕截图

- (UIImage *)getNormalImage:(UIView *)view {

    UIGraphicsBeginImageContext(CGSizeMake(ScreenWidth, ScreenHeight));

    CGContextRef context = UIGraphicsGetCurrentContext();

    [view.layer renderInContext:context];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    returnimage;

}

这里传入的view应该是self.navigationController.view,如果传self.view 你获得的截图里没有导航栏

第二步:给截图设置毛玻璃参数

 UIImageView *blur_imgv = [[UIImageView alloc]initWithFrame:self.BgView.bounds];

    UIImage *im = [self getNormalImage:view];

    blur_imgv.image = [im applyBlurWithRadius:2 tintColor:[UIColor colorWithWhite:1 alpha:0.54] saturationDeltaFactor:0 maskImage:nil];

    [self.BgViewaddSubview:blur_imgv];

self.bgView为下面的灰色view 这里也可以直接加在你的自定义view上 frame和self.bgView设置一样 

applyBlurWithRadius这个方法是我自己整理的一个第三方里的 :#import "UIImage+ImageEffects.h",参数blurRadius:代表模糊程度,tintColor背景颜色,saturation:饱和度

applyBlurWithRadius这个方法要导入一个库:Accelerate.framework

这个第三方的.h .m如下(ps:网上找来的)

.h

#import

@import UIKit;

@interfaceUIImage (ImageEffects)

-(UIImage*)applyLightEffect;

-(UIImage*)applyExtraLightEffect;

-(UIImage*)applyDarkEffect;

-(UIImage*)applyTintEffectWithColor:(UIColor*)tintColor;

-(UIImage*)applyBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor*)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage*)maskImage;

@end


.m

#import

@implementationUIImage (ImageEffects)

-(UIImage*)applyLightEffect {


    UIColor*tintColor =[UIColor colorWithWhite:1.0 alpha:0.3];


    return[self applyBlurWithRadius:30 tintColor:tintColor saturationDeltaFactor:1.8 maskImage:nil];

}

-(UIImage*)applyExtraLightEffect {


    UIColor*tintColor =[UIColor colorWithWhite:0.97 alpha:0.82];


    return[self applyBlurWithRadius:20 tintColor:tintColor saturationDeltaFactor:1.8 maskImage:nil];

}

-(UIImage*)applyDarkEffect {


    UIColor*tintColor =[UIColor colorWithWhite:0.11 alpha:0.73];


    return[self applyBlurWithRadius:20 tintColor:tintColor saturationDeltaFactor:1.8 maskImage:nil];

}

-(UIImage*)applyTintEffectWithColor:(UIColor*)tintColor {


    constCGFloatEffectColorAlpha =0.6;

    UIColor*effectColor = tintColor;

    size_t componentCount = CGColorGetNumberOfComponents(tintColor.CGColor);

    if(componentCount ==2) {

        CGFloatb;

        if([tintColorgetWhite:&balpha:NULL]) {

            effectColor = [UIColorcolorWithWhite:balpha:EffectColorAlpha];

        }

    }else{

        CGFloatr, g, b;

        if([tintColorgetRed:&rgreen:&gblue:&balpha:NULL]) {

            effectColor = [UIColorcolorWithRed:rgreen:gblue:balpha:EffectColorAlpha];

        }

    }

    return[self applyBlurWithRadius:10 tintColor:effectColor saturationDeltaFactor:-1.0 maskImage:nil];

}

-(UIImage*)applyBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor*)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage*)maskImage {


    //  Check pre-conditions. 检查前提条件

    if(self.size.width <1||self.size.height <1){

        NSLog(@"*** error: invalid size: (%.2f x %.2f). Both dimensions must be >= 1: %@",self.size.width,self.size.height,self);

        returnnil;

    }

    if(!self.CGImage){

        NSLog(@"*** error: image must be backed by a CGImage: %@",self);

        returnnil;

    }

    if(maskImage &&!maskImage.CGImage){

        NSLog(@"*** error: maskImage must be backed by a CGImage: %@", maskImage);

        returnnil;

    }


    CGRectimageRect = {CGPointZero,self.size};

    UIImage*effectImage =self;

    BOOLhasBlur = blurRadius > __FLT_EPSILON__;

    BOOLhasSaturationChange =fabs(saturationDeltaFactor -1.)> __FLT_EPSILON__;

    if(hasBlur || hasSaturationChange) {


        UIGraphicsBeginImageContextWithOptions(self.size, NO,[[UIScreen mainScreen] scale]);

        CGContextRef effectInContext = UIGraphicsGetCurrentContext();

        CGContextScaleCTM(effectInContext,1.0,-1.0);

        CGContextTranslateCTM(effectInContext,0,-self.size.height);

        CGContextDrawImage(effectInContext,imageRect,self.CGImage);

        vImage_BuffereffectInBuffer;

        effectInBuffer.data=CGBitmapContextGetData(effectInContext);

        effectInBuffer.width=CGBitmapContextGetWidth(effectInContext);

        effectInBuffer.height=CGBitmapContextGetHeight(effectInContext);

        effectInBuffer.rowBytes=CGBitmapContextGetBytesPerRow(effectInContext);

        UIGraphicsBeginImageContextWithOptions(self.size, NO, [[UIScreen mainScreen] scale]);

        CGContextRef effectOutContext = UIGraphicsGetCurrentContext();

        vImage_BuffereffectOutBuffer;

        effectOutBuffer.data=CGBitmapContextGetData(effectOutContext);

        effectOutBuffer.width=CGBitmapContextGetWidth(effectOutContext);

        effectOutBuffer.height=CGBitmapContextGetHeight(effectOutContext);

        effectOutBuffer.rowBytes=CGBitmapContextGetBytesPerRow(effectOutContext);

        if(hasBlur){

            // A description of how to compute the box kernel width from the Gaussian

            // radius (aka standard deviation) appears in the SVG spec:

            // http://www.w3.org/TR/SVG/filters.html#feGaussianBlurElement

            //

            // For larger values of 's' (s >= 2.0), an approximation can be used: Three

            // successive box-blurs build a piece-wise quadratic convolution kernel, which

            // approximates the Gaussian kernel to within roughly 3%.

            //

            // let d = floor(s * 3*sqrt(2*pi)/4 + 0.5)

            //

            // ... if d is odd, use three box-blurs of size 'd', centered on the output pixel.

            //

            CGFloatinputRadius = blurRadius *[[UIScreenmainScreen]scale];

            NSUIntegerradius =floor(inputRadius *3.*sqrt(2* M_PI)/4+0.5);

            if(radius %2!=1) {

                radius +=1;// force radius to be odd so that the three box-blur methodology works.

            }

            vImageBoxConvolve_ARGB8888(&effectInBuffer,&effectOutBuffer,NULL,0,0, (uint32_t)radius, (uint32_t)radius,0,kvImageEdgeExtend);

            vImageBoxConvolve_ARGB8888(&effectOutBuffer,&effectInBuffer,NULL,0,0, (uint32_t)radius, (uint32_t)radius,0,kvImageEdgeExtend);

            vImageBoxConvolve_ARGB8888(&effectInBuffer,&effectOutBuffer,NULL,0,0, (uint32_t)radius, (uint32_t)radius,0,kvImageEdgeExtend);

        }

        BOOLeffectImageBuffersAreSwapped =NO;

        if(hasSaturationChange) {

            CGFloats = saturationDeltaFactor;

            CGFloatfloatingPointSaturationMatrix[] = {

                0.0722+0.9278* s,0.0722-0.0722* s,0.0722-0.0722* s,0,

                0.7152-0.7152* s,0.7152+0.2848* s,0.7152-0.7152* s,0,

                0.2126-0.2126* s,0.2126-0.2126* s,0.2126+0.7873* s,0,

                0,0,0,1,

            };

            constint32_tdivisor =256;

            NSUIntegermatrixSize =sizeof(floatingPointSaturationMatrix)/sizeof(floatingPointSaturationMatrix[0]);

            int16_tsaturationMatrix[matrixSize];

            for(NSUIntegeri =0; i < matrixSize; ++i) {

                saturationMatrix[i] = (int16_t)roundf(floatingPointSaturationMatrix[i]* divisor);

            }

            if(hasBlur) {

                vImageMatrixMultiply_ARGB8888(&effectOutBuffer,&effectInBuffer, saturationMatrix, divisor,NULL,NULL,kvImageNoFlags);

                effectImageBuffersAreSwapped =YES;

            }else{

                vImageMatrixMultiply_ARGB8888(&effectInBuffer,&effectOutBuffer, saturationMatrix, divisor,NULL,NULL,kvImageNoFlags);

            }

        }

        if(!effectImageBuffersAreSwapped)

            effectImage =UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        if(effectImageBuffersAreSwapped)

            effectImage =UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

    }

    // Set up output context. 设置输出上下文

    UIGraphicsBeginImageContextWithOptions(self.size, NO, [[UIScreen mainScreen] scale]);

    CGContextRef outputContext = UIGraphicsGetCurrentContext();

    CGContextScaleCTM(outputContext,1.0, -1.0);

    CGContextTranslateCTM(outputContext, 0, -self.size.height);

    // Draw base image. 绘制基准图像

    CGContextDrawImage(outputContext, imageRect,self.CGImage);

    // Draw effect image. 绘制效果图像

    if(hasBlur) {

        CGContextSaveGState(outputContext);

        if(maskImage) {

            CGContextClipToMask(outputContext, imageRect, maskImage.CGImage);

        }

        CGContextDrawImage(outputContext, imageRect, effectImage.CGImage);

        CGContextRestoreGState(outputContext);

    }

    // Add in color tint. 添加颜色渲染

    if(tintColor) {

        CGContextSaveGState(outputContext);

        CGContextSetFillColorWithColor(outputContext, tintColor.CGColor);

        CGContextFillRect(outputContext, imageRect);

        CGContextRestoreGState(outputContext);

    }

    // Output image is ready. 输出图像

    UIImage*outputImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    returnoutputImage;

}

@end

你可能感兴趣的:(如何给毛玻璃设置它的模糊程度blur)