【问题标题】:How to blur image using CALayer如何使用 CALayer 模糊图像
【发布时间】:2013-06-20 20:53:39
【问题描述】:

以下方法尝试将高斯模糊应用于图像。但是它什么也没做。你能告诉我哪里出了问题,如果你也知道错误的原因,那也会有帮助。我正在尝试了解 CALayers 和quartzcore。

谢谢

-(void)updateFavoriteRecipeImage{

    [self.favoriteRecipeImage setImageWithURL:[NSURL URLWithString:self.profileVCModel.favoriteRecipeImageUrl] placeholderImage:[UIImage imageNamed:@"miNoImage"]];

    //Set content mode
    [self.favoriteRecipeImage setContentMode:UIViewContentModeScaleAspectFill];
    self.favoriteRecipeImage.layer.masksToBounds = YES;

    //Blur the image
    CALayer *blurLayer = [CALayer layer];
    CIFilter *blur = [CIFilter filterWithName:@"CIGaussianBlur"];
    [blur setDefaults];
    blurLayer.backgroundFilters = [NSArray arrayWithObject:blur];
    [self.favoriteRecipeImage.layer addSublayer:blurLayer];


    [self.favoriteRecipeImage setAlpha:0];

    //Show image using fade
    [UIView animateWithDuration:.3 animations:^{

        //Load alpha
        [self.favoriteRecipeImage setAlpha:1];
        [self.favoriteRecipeImageMask setFrame:self.favoriteRecipeImage.frame];
    }];
}

【问题讨论】:

    标签: ios core-animation quartz-core


    【解决方案1】:

    documentation of the backgroundFilters property 是这样说的:

    特殊注意事项

    iOS 中的图层不支持此属性。

    从 iOS 6.1 开始,iOS 上没有用于将实时过滤器应用于图层的公共 API。您可以编写代码将底层图层绘制到CGImage,然后对该图像应用滤镜并将其设置为图层的背景,但这样做有点复杂并且不是“实时”的(如果底层发生变化)。

    【讨论】:

    • 就像 Rob 所说的,最好的选择是将要模糊的图层渲染为 CGImage,然后对其应用核心图像模糊滤镜。
    • @waf GPUImage 目前有 125 种不同的滤镜,包括 GPUImageFastBlurFilter,这是一种硬件加速的高斯模糊。
    • 使用filters 属性而不是backgroundFilters 怎么样?文档中没有任何说明它在 iOS 上不可用。然而,它仍然没有明显的效果。有人知道为什么吗?
    • @robmayoff 对,不知怎的,我错过了。对不起
    【解决方案2】:

    试试下面的方法:

    CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"test.png"]] ;
    
    CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"] ;
    
    [blurFilter setDefaults] ;
    
    [blurFilter setValue:inputImage forKey:@"inputImage"] ;
    
    [blurFilter setValue: [NSNumber numberWithFloat:10.0f] forKey:@"inputRadius"];
    
    CIImage *outputImage = [blurFilter valueForKey: @"outputImage"];
    
    CIContext *context = [CIContext contextWithOptions:nil];
    
    self.bluredImageView.image = [UIImage imageWithCGImage:[context createCGImage:outputImage fromRect:outputImage.extent]];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      相关资源
      最近更新 更多