当前位置:主页 > 移动开发 > iOS代码 >

iOS实现背景高斯模糊效果

时间:2021-10-30 10:33:08 | 栏目:iOS代码 | 点击:

废话不多说,实现简单的高斯模糊的效果代码:

  UIView *bgview= [[UIViewalloc]initWithFrame:self.view.frame];
//  bgview.backgroundColor = [UIColor blackColor];
//  bgview.alpha = 0.9;
  bgview.tag =10086;
  [self.viewaddSubview:bgview];
  UIBlurEffect *blur = [UIBlurEffecteffectWithStyle:UIBlurEffectStyleLight];
  UIVisualEffectView *effectview = [[UIVisualEffectViewalloc] initWithEffect:blur];
  effectview.frame =self.view.frame;
  [bgview addSubview:effectview];

这写代码只是简单的实现高斯模糊。关于消除,把它remove就行了。

关于图片的高斯模糊,首先拿到图片:

CIContext *context = [CIContext contextWithOptions:nil]; 
  CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"1.png"]]; 
  // create gaussian blur filter 
  CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"]; 
  [filter setValue:inputImage forKey:kCIInputImageKey]; 
  [filter setValue:[NSNumber numberWithFloat:10.0] forKey:@"inputRadius"]; 
  // blur image 
  CIImage *result = [filter valueForKey:kCIOutputImageKey]; 
  CGImageRef cgImage = [context createCGImage:result fromRect:[result extent]]; 
  UIImage *image = [UIImage imageWithCGImage:cgImage]; 
  CGImageRelease(cgImage); 
  self.mainImageView.image = image; 

您可能感兴趣的文章:

相关文章