iPhone SDK – 磨砂玻璃(iOS 7模糊)效果

我正在尝试在UIImageView中应用磨砂玻璃效果

我试图实现我在这个问题中发现的,但结果是不能接受的。 我想要这样的东西:

磨砂玻璃效果

有任何想法吗?

更新:

另外,我们可以看到iOS 7在很多地方使用这种效果。 我们如何重现呢?

关于CoreImage的一个很好的教程在这里,展示了如何应用filter和更多:

http://www.raywenderlich.com/5689/beginning-core-image-in-ios-5

更新1

所以经过一番研究之后,我发现与iOS OS X版本相比,iOS的Core Image还不完整。 所以我search了很多,我find了两个解决scheme,其中一个更简单,另一个更广泛和更复杂的库。

  • 简单而简短的解决scheme: https : //github.com/esilverberg/ios-image-filters

  • 令人惊叹的编辑价值,用于处理图像和video的库,由Brad Larson提供的OpenGL,GPUImage。 它比Core Image快得多,效率也高。 简介: http : //www.sunsetlakesoftware.com/2012/02/12/introducing-gpuimage-framework 。 GitHub: GPUImage

所以,例如,在几行我可以得到我想要的结果(其中originalImage是应用效果的UIImage):

GPUImageGaussianBlurFilter *blurFilter = [[GPUImageGaussianBlurFilter alloc] init]; blurFilter.blurSize = 2; UIImage *blurImage = [blurFilter imageByFilteringImage:resizedImage]; 

更新2

在苹果宣布iOS 7之后,一些开发者发现了一种解决方法,就像苹果公司在默认的iOS应用程序中一样,因为苹果公司没有提供API。 在我看来,最简单和最好的解决scheme就是这个 。 为什么我认为这是最好的? 因为即使背后的一些看法发生了变化,模糊效果仍然很好,因为我们预计它会起作用。 但是,请记住,它依赖于iOS 7 SDK才能工作,如果Apple更改了UIToolbar,则可能会有风险。

更新3

苹果在WWDC 2013上提到,他们会在UIImage上提供一个名为UIImage + ImageEffects的类别类(我使用了它, 并在这里find ,但在Developer Portal中可用- 在UIImage中searchUIImageEffectssearch框 )。 使用这个类别,您可以使用多种方法(浅色,深色,特定颜色等)在静态UIImage中应用模糊。 此外,昨天我看到这个组件 ,发现它很有趣,因为你可以在一个框架中应用效果(基于上述类别)。

更新4

最后,在iOS 8上,苹果发布了可以轻松实现模糊效果的新类。 借助UIVisualEffectUIVisualEffectView ,您可以快速将实时模糊添加到您的应用程序。 这里是Ryan Nystrom关于如何使用这些类的一个很好的教程(并且通常是模糊的) :

iOS 7和8的解决scheme,根本不使用CoreImage或CoreGraphics:

 - (void)addBlurToView:(UIView *)view { UIView *blurView = nil; if([UIBlurEffect class]) { // iOS 8 UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]; blurView = [[UIVisualEffectView alloc] initWithEffect:blurEffect]; blurView.frame = view.frame; } else { // workaround for iOS 7 blurView = [[UIToolbar alloc] initWithFrame:view.bounds]; } [blurView setTranslatesAutoresizingMaskIntoConstraints:NO]; [view addSubview:blurView]; [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[blurView]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(blurView)]]; [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[blurView]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(blurView)]]; } 

(假设您的目标版本不是iOS 7之前的版本;如果是,则必须在else版本中testingiOS版本)

这个解决scheme适用于任何视图,而不仅仅是图像。

使用UIImageEffects

如果您在应用模糊时需要更多控制权,则可以使用Apple的UIImageEffects (可通过其示例代码获得)。

您可以从Apple的开发库复制UIImageEffects的代码: 模糊和着色图像

以下是如何使用它:

 #import "UIImageEffects.h" ... self.yourImageView.image = [UIImageEffects imageByApplyingLightEffectToImage:[UIImage imageNamed:@"yourImage.png"]]; 

看看核心图像编程指南 。 似乎风格化filter和模糊filter可能适合您的需求。 我以前从来没有使用Core Image,但我认为可能会有一些很好的WWDC会话。 文档中有一个基本的示例代码 。