ios 如何将模糊应用于 UIView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11130923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:52:28  来源:igfitidea点击:

How to apply blur to a UIView?

iosimage-processinguiviewblur

提问by virindh

Right now I am building an iPhone app that requires blurring an entire UIView. How can I achieve this? I have seen this framework, but I don't think that works with UIView. Is there an alternate way to blur an UIView?

现在我正在构建一个需要模糊整个 UIView 的 iPhone 应用程序。我怎样才能做到这一点?我见过这个框架,但我认为它不适用于 UIView。有没有其他方法可以模糊 UIView?

UPDATE: Check for my updated answer below, adding more relevance with the advent of iOS 7 and iOS 8.

更新:检查下面我更新的答案,增加与 iOS 7 和 iOS 8 的出现的更多相关性。

采纳答案by virindh

Ever since the release of iOS 7, This question has been getting a lot of hits and I thought I should follow up on what I ended up doing.

自从 iOS 7 发布以来,这个问题一直受到很多点击,我想我应该跟进我最终做了什么。

I personally blurred the picture at the time with photoshop, but there are many great third party libraries available that do dynamicand staticblurring, here are a couple:

当时我个人用 photoshop 模糊了图片,但是有很多很棒的第三方库可以做动态静态模糊,这里有几个:

I wanted to post this because you no longer needto take screenshots of individual frames or screen.

我想发布这个是因为您不再需要对单个帧或屏幕进行截图。

iOS 8:With the coming release of iOS 8, Apple has included a built in blurred UIView, UIVisualEffectView(https://developer.apple.com/documentation/uikit/uivisualeffectview)

iOS 8:随着即将发布的 iOS 8,Apple 包含了一个内置的 blurred UIViewUIVisualEffectViewhttps://developer.apple.com/documentation/uikit/uivisualeffectview

回答by pasawaya

This should work. I commented in the code to help you understand what's going on:

这应该有效。我在代码中进行了注释,以帮助您了解发生了什么:

//To take advantage of CIFilters, you have to import the Core Image framework
#import <CoreImage/CoreImage.h>

//Get a UIImage from the UIView
UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//Blur the UIImage with a CIFilter
CIImage *imageToBlur = [CIImage imageWithCGImage:viewImage.CGImage];    
CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"]; 
[gaussianBlurFilter setValue:imageToBlur forKey: @"inputImage"]; 
[gaussianBlurFilter setValue:[NSNumber numberWithFloat: 10] forKey: @"inputRadius"];
CIImage *resultImage = [gaussianBlurFilter valueForKey: @"outputImage"]; 
UIImage *endImage = [[UIImage alloc] initWithCIImage:resultImage];

//Place the UIImage in a UIImageView
UIImageView *newView = [[UIImageView alloc] initWithFrame:self.view.bounds];
newView.image = endImage;
[self.view addSubview:newView];

If you have any questions about the code, just leave it in the comments.

如果您对代码有任何疑问,请在评论中留言。

Note:CIGaussianBlur isn't present on iOS as of 5.1, so you must find a different way to blur the view for devices 5.x+ (Thanks to @BradLarson for this tip). The accepted answer in this questionlooks promising as a replacement, as does this library.

注意:从 5.1 开始,iOS 上不存在 CIGaussianBlur,因此您必须找到一种不同的方法来模糊设备 5.x+ 的视图(感谢 @BradLarson 提供此提示)。这个问题中接受的答案看起来很有希望作为替代品,这个库也是如此

回答by Robert

In iOS 8 you can use UIVisualEffectViewto get blurred Views. See: https://developer.apple.com/documentation/uikit/uivisualeffectview

在 iOS 8 中,您可以使用UIVisualEffectView模糊视图。请参阅:https: //developer.apple.com/documentation/uikit/uivisualeffectview

回答by Yano

Simply solved using this code.

使用此代码简单解决。

UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:yourView.bounds];
toolBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 7.0) {
    toolBar.barTintColor = nil;
    toolBar.translucent = YES;
    toolBar.barStyle = UIBarStyleBlack;
}
else
    [toolBar setTintColor:[UIColor colorWithRed:5 green:31 blue:75 alpha:1]];
[yourView insertSubview:toolBar atIndex:0];

回答by YaBoiSandeep

The simplest way to blur a view is to add UIToolbar,just change the alpha value to change the blur.

模糊视图的最简单方法是添加 UIToolbar,只需更改 alpha 值即可更改模糊。

self.toolbar = [[UIToolbar alloc]initForAutoLayout];
[self.view addSubview:self.toolbar];
[self.toolbar autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
[self.toolbar autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
[self.toolbar autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:NAVBAR_HEIGHT];
[self.toolbar autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0];
self.toolbar.alpha = 0.5;