如何使用UIVisualEffectView来模糊图像?

有人能给一个小例子,应用模糊的图像?我一直在努力弄清楚代码现在:(仍然是新的在obj c!

UIVisualEffectView提供了对复杂的简单抽象 视觉效果。根据所期望的效果,结果可能会 影响视图后面分层的内容或添加到视图的内容 contentView . < / p > 将UIVisualEffectView应用到现有视图以应用模糊或 对当前视图的活力效果。在添加 UIVisualEffectView到视图层次结构中,添加任何子视图到 UIVisualEffectView. contentView。不直接添加子视图 UIVisualEffectView本身。

https://developer.apple.com/documentation/uikit/uivisualeffectview#//apple_ref/occ/instp/UIVisualEffectView/contentView

160606 次浏览

把这个模糊视图放到imageView上。下面是Objective-C中的一个例子:

UIVisualEffect *blurEffect;
blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];


UIVisualEffectView *visualEffectView;
visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];


visualEffectView.frame = imageView.bounds;
[imageView addSubview:visualEffectView];

迅速:

var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light))


visualEffectView.frame = imageView.bounds


imageView.addSubview(visualEffectView)

这里是如何使用UIVibrancyEffect和UIBlurEffect与UIVisualEffectView

objective - c:

// Blur effect
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
[blurEffectView setFrame:self.view.bounds];
[self.view addSubview:blurEffectView];


// Vibrancy effect
UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:blurEffect];
UIVisualEffectView *vibrancyEffectView = [[UIVisualEffectView alloc] initWithEffect:vibrancyEffect];
[vibrancyEffectView setFrame:self.view.bounds];


// Label for vibrant text
UILabel *vibrantLabel = [[UILabel alloc] init];
[vibrantLabel setText:@"Vibrant"];
[vibrantLabel setFont:[UIFont systemFontOfSize:72.0f]];
[vibrantLabel sizeToFit];
[vibrantLabel setCenter: self.view.center];


// Add label to the vibrancy view
[[vibrancyEffectView contentView] addSubview:vibrantLabel];


// Add the vibrancy view to the blur view
[[blurEffectView contentView] addSubview:vibrancyEffectView];

迅速:

// Blur Effect
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
view.addSubview(blurEffectView)


// Vibrancy Effect
let vibrancyEffect = UIVibrancyEffect(blurEffect: blurEffect)
let vibrancyEffectView = UIVisualEffectView(effect: vibrancyEffect)
vibrancyEffectView.frame = view.bounds


// Label for vibrant text
let vibrantLabel = UILabel()
vibrantLabel.text = "Vibrant"
vibrantLabel.font = UIFont.systemFont(ofSize: 72.0)
vibrantLabel.sizeToFit()
vibrantLabel.center = view.center


// Add label to the vibrancy view
vibrancyEffectView.contentView.addSubview(vibrantLabel)


// Add the vibrancy view to the blur view
blurEffectView.contentView.addSubview(vibrancyEffectView)

您还可以使用界面构建器为简单的情况轻松地创建这些效果。由于视图的z值取决于它们在文档大纲中列出的顺序,所以可以在要模糊的视图之前将UIVisualEffectView拖到文档大纲上。这将自动创建一个嵌套的UIView,它是给定UIVisualEffectViewcontentView属性。在这个视图中嵌套你想要出现在模糊顶部的东西。

XCode6 beta5

你也可以很容易地利用活力UIVisualEffect,它会自动在文档大纲中创建另一个嵌套的UIVisualEffectView,默认启用了活力。然后,您可以向嵌套的UIView(同样是UIVisualEffectViewcontentView属性)添加一个标签或文本视图,以实现与“>滑动解锁”UI元素相同的效果。

enter image description here

我更喜欢通过故事板来创建视觉效果——没有代码用于创建或维护UI元素。它还为我提供了完整的横向支持。我已经做了一个使用UIVisualEffects模糊和动态的小演示。

Demo on Github

如果有人想要Swift的答案:

var blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark) // Change .Dark into .Light if you'd like.


var blurView = UIVisualEffectView(effect: blurEffect)


blurView.frame = theImage.bounds // 'theImage' is an image. I think you can apply this to the view too!

更新:

到目前为止,它是在IB下可用的,所以你不需要为它编写任何代码:)

-(void) addBlurEffectOverImageView:(UIImageView *) _imageView
{
UIVisualEffect *blurEffect;
blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];


UIVisualEffectView *visualEffectView;
visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];


visualEffectView.frame = _imageView.bounds;
[_imageView addSubview:visualEffectView];
}