使用iOS 7的快照API模糊屏幕 [英] Blur screen with iOS 7's snapshot API

查看:133
本文介绍了使用iOS 7的快照API模糊屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信NDA会下降,所以我可以问这个问题。我有一个UIView子类:

I believe the NDA is down, so I can ask this question. I have a UIView subclass:

BlurView *blurredView = ((BlurView *)[self.view snapshotViewAfterScreenUpdates:NO]);
blurredView.frame = self.view.frame;
[self.view addSubview:blurredView];

到目前为止,它在捕获屏幕方面做得很好,但现在我想模糊那个视图。我到底该怎么做?根据我的阅读,我需要捕获视图的当前内容(上下文?!)并将其转换为CIImage(没有?),然后对其应用CIGaussianBlur并将其绘制回视图。

It does its job so far in capturing the screen, but now I want to blur that view. How exactly do I go about this? From what I've read I need to capture the current contents of the view (context?!) and convert it to CIImage (no?) and then apply a CIGaussianBlur to it and draw it back on the view.

我该怎么做?

PS该视图不是动画,所以它应该是性能明智的。

P.S. The view is not animated, so it should be OK performance wise.

编辑:这是我到目前为止所拥有的。问题是我无法将快照捕获到UIImage,我得到一个黑屏。但如果我直接将视图添加为子视图,我可以看到快照在那里。

Here is what I have so far. The problem is that I can't capture the snapshot to a UIImage, I get a black screen. But if I add the view as a subview directly, I can see the snapshot is there.

// Snapshot
UIView *view = [self.view snapshotViewAfterScreenUpdates:NO];

// Convert to UIImage
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// Apply the UIImage to a UIImageView
BlurView *blurredView = [[BlurView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];
[self.view addSubview:blurredView];
blurredView.imageView.image = img;

// Black screen -.-

BlurView.m:

BlurView.m:

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];

    if (self) {
        // Initialization code
        self.imageView = [[UIImageView alloc] init];
        self.imageView.frame = CGRectMake(20, 20, 200, 200);
        [self addSubview:self.imageView];
    }
    return self;
}


推荐答案

来自WWDC的示例代码 ios_uiimageeffects

Sample Code from WWDC ios_uiimageeffects

有a UIImage 类别名为 UIImage + ImageEffects

这是它的API:

- (UIImage *)applyLightEffect;
- (UIImage *)applyExtraLightEffect;
- (UIImage *)applyDarkEffect;
- (UIImage *)applyTintEffectWithColor:(UIColor *)tintColor;

- (UIImage *)applyBlurWithRadius:(CGFloat)blurRadius 
                       tintColor:(UIColor *)tintColor 
           saturationDeltaFactor:(CGFloat)saturationDeltaFactor 
                       maskImage:(UIImage *)maskImage;

由于法律原因,我无法在此处显示实施,其中有一个演示项目。应该很容易开始。

For legal reason I can't show the implementation here, there is a demo project in it. should be pretty easy to get start with.

这篇关于使用iOS 7的快照API模糊屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆