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

查看:18
本文介绍了使用 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.

我该怎么做?

附言该视图没有动画,因此在性能方面应该没问题.

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

这是我目前所拥有的.问题是我无法将快照捕获到 UIImage,出现黑屏.但是如果我直接把view添加为subview,就可以看到快照在那里了.

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

有一个名为 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天全站免登陆