高斯模糊图像 - iOS 8

Kyl*_*yle 3 objective-c gaussian ios

我有一个移动的背景图像,我想模糊它的底部.我用Photoshop完成它,但是由于图像移动,它将无法正常工作.

这就是我的意思(看看图像的底部):

http://i.imgur.com/OT3vpeQ.png

所以基本上就像Dock对iPhone的影响一样.我使用的是iOS 8而不是Swift.

San*_*eep 6

我根据你那里的照片做了一个小例子.我的算法如下:

  • 从底部提取一部分图像.
  • 对它应用高斯滤波器并模糊它.
  • 然后,创建一个新的图像上下文,在其上绘制原始图像.
  • 然后,绘制图像的模糊部分,使其准确地放在原始图像上.
  • 从上下文中提取新图像.

这是一个这样做的源代码,

 @implementation ViewController


- (void)viewDidLoad{

  [super viewDidLoad];
  UIImageView *imageView = [[UIImageView alloc] init];
  imageView.frame = self.view.bounds;
  [self.view  addSubview:imageView];
  imageView.contentMode = UIViewContentModeScaleAspectFit;
  UIImage *image = [UIImage imageNamed:@"monogram.jpg"];
  imageView.image = [self imageWithBlurredImageWithImage: image andBlurInsetFromBottom: 200 withBlurRadius:3];
}

- (UIImage*)imageWithBlurredImageWithImage:(UIImage*)image andBlurInsetFromBottom:(CGFloat)bottom withBlurRadius:(CGFloat)blurRadius{
  UIGraphicsBeginImageContext(image.size);
  CGContextRef context =  UIGraphicsGetCurrentContext();
  CGContextScaleCTM(context, 1, -1);
  CGContextTranslateCTM(context, 0, -image.size.height);
  CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
  CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, bottom), [self blurImage: image withBottomInset: bottom blurRadius: blurRadius].CGImage);
  image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return image;
}

- (UIImage*)blurImage:(UIImage*)image withBottomInset:(CGFloat)inset blurRadius:(CGFloat)radius{

  image =  [UIImage imageWithCGImage: CGImageCreateWithImageInRect(image.CGImage, CGRectMake(0, image.size.height - inset, image.size.width,inset))];

  CIImage *ciImage = [CIImage imageWithCGImage:image.CGImage];
  CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
  [filter setValue:ciImage forKey:kCIInputImageKey];
  [filter setValue:@(radius) forKey:kCIInputRadiusKey];

  CIImage *outputCIImage = filter.outputImage;
  CIContext *context = [CIContext contextWithOptions:nil];

  return [UIImage imageWithCGImage: [context createCGImage:outputCIImage fromRect:ciImage.extent]];

}
@end
Run Code Online (Sandbox Code Playgroud)

这是结果的屏幕截图.

在此输入图像描述

  • @JesseRusak你不仅不能控制半径,而且你也被迫使图像更亮或更暗..:o (7认同)
  • 为什么不直接使用iOS 8 UIVisualEffectView? (4认同)
  • 我知道,但你能用UIVisualEffectView控制模糊半径吗? (4认同)
  • 正如Jesse建议退房:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIVisualEffectView/index.html#//apple_ref/occ/instp/UIVisualEffectView/effect (2认同)