当前位置: 首页 > 工具软件 > UIView-Blur > 使用案例 >

怎么给UIView设置模糊效果

姜奇
2023-12-01

@interface UIView (Effects)


- (void)blur;

- (void)unBlur;


@end




@implementation UIView (Effects)


- (void)blur{

    UIGraphicsBeginImageContext(self.bounds.size);

    [self.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    CIContext *context = [CIContext contextWithOptions:nil];


    CIImage *imageToBlur = [CIImage imageWithCGImage:viewImage.CGImage];

    CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];

    [gaussianBlurFilter setValue:imageToBlur forKey: @"inputImage"];

    [gaussianBlurFilter setValue:[NSNumber numberWithFloat: 15] forKey: @"inputRadius"];

    CIImage *resultImage = [gaussianBlurFilter valueForKey: @"outputImage"];

    

    CGImageRef cgImage = [context createCGImage:resultImage fromRect:self.bounds];

    UIImage *blurredImage = [UIImage imageWithCGImage:cgImage];

    CFRelease(cgImage);


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

    imageView.tag = -1;

    imageView.image = blurredImage;

    

    UIView *overlay = [[UIView alloc] initWithFrame:self.bounds];

    overlay.tag = -2;

    overlay.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.1];

    //overlay.backgroundColor = [UIColor clearColor];

    //overlay.alpha = 0.1;

    [self addSubview:imageView];

    [self addSubview:overlay];

    

    [imageView release];

    [overlay release];

}


-(void)unBlur{

    [[self viewWithTag:-1] removeFromSuperview];

    [[self viewWithTag:-2] removeFromSuperview];

}


 

@end

 类似资料: