Ios 如何使用CALayer模糊图像
以下方法尝试对图像应用高斯模糊。然而,它什么也没做。你能告诉我什么是错的吗?如果你也知道它错的原因,那也会有帮助。我正在努力学习CALayers和quartzcore 谢谢Ios 如何使用CALayer模糊图像,ios,core-animation,quartz-core,Ios,Core Animation,Quartz Core,以下方法尝试对图像应用高斯模糊。然而,它什么也没做。你能告诉我什么是错的吗?如果你也知道它错的原因,那也会有帮助。我正在努力学习CALayers和quartzcore 谢谢 -(void)updateFavoriteRecipeImage{ [self.favoriteRecipeImage setImageWithURL:[NSURL URLWithString:self.profileVCModel.favoriteRecipeImageUrl] placeholderImage:
-(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中的层不支持此属性
从iOS 6.1开始,没有用于将实时过滤器应用于iOS上的层的公共API。您可以编写代码将基础层绘制到CGImage
,然后对该图像应用过滤器并将其设置为层的背景,但这样做有点复杂,并且不是“活动的”(如果基础层发生更改,它不会自动更新)。说明:
特别注意事项
iOS中的层不支持此属性
从iOS 6.1开始,没有用于将实时过滤器应用于iOS上的层的公共API。您可以编写代码将基础层绘制到
CGImage
,然后对该图像应用过滤器并将其设置为层的背景,但这样做有点复杂,并且不是“活动的”(如果基础层发生更改,它不会自动更新)。尝试以下操作:
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]];
请尝试以下操作:
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]];
正如Rob所说,最好的选择是将要模糊的层渲染为
CGImage
,然后对其应用核心图像模糊过滤器。@waf目前有125个不同的过滤器,包括硬件加速高斯模糊的GPUImageFastBlurFilter
。使用filters
属性而不是backgroundFilters
怎么样?文档中没有任何关于iOS上不可用的注释。然而,它仍然没有明显的效果。有人知道为什么吗?@drasto@robmayoff是的,不知为什么我错过了。SorryLike Rob说,最好的替代方法是将要模糊的层渲染为CGImage
,然后对其应用核心图像模糊过滤器。@waf目前有125个不同的过滤器,包括硬件加速高斯模糊的GPUImageFastBlurFilter
。使用filters
属性而不是backgroundFilters
怎么样?文档中没有任何关于iOS上不可用的注释。然而,它仍然没有明显的效果。有人知道为什么吗?@drasto@robmayoff是的,不知为什么我错过了。很抱歉