不使用 renderInContext 捕获 UIView: [英] Capture UIView without using renderInContext:

查看:41
本文介绍了不使用 renderInContext 捕获 UIView:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的屏幕创建视频,为此我将捕获视图,但由于某种原因我不想使用 renderInContext:.我现在正在使用 drawViewHierarchyInRect:,但这仅限于 iOS 7,我的应用程序也支持早期的 iO​​S 版本.使用 drawViewHierarchyInRect: 会被罚款吗?

I want to create video from my screen and for that I'm going to capture the view but I don't want to use renderInContext: due to some reason. I'm using now drawViewHierarchyInRect:, but this is limited to iOS 7 and my app supports earlier iOS versions, too. Will I be fined for using drawViewHierarchyInRect:?

这是我的代码

UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

推荐答案

我知道您不想使用 renderInContext 因为它效率较低,但对于 7 之前的 iOS 版本,那就是您应该使用的技术(因为如果您尝试在 7.0 之前的 iOS 版本中使用 drawViewHierarchyInRect,应用程序将崩溃).因此,这是一个在可用时使用 drawViewHierarchyInRect 而在不可用时使用 renderInContext 的再现:

I understand that you don't want to use renderInContext because it is less efficient, but for iOS versions prior to 7, that's the technique you should use (because if you attempt to use drawViewHierarchyInRect in iOS versions prior to 7.0, the app will crash). So, here is a rendition that uses drawViewHierarchyInRect when it's available, but renderInContext when not:

UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);

if ([self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
    BOOL success = [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
    NSAssert(success, @"drawViewHierarchyInRect failed");
} else {
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
}

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

这样,您将在 iOS 7+ 上使用更高效的机制,但至少在早期版本上运行时不会崩溃.

That way, you'll use the more efficient mechanism for iOS 7+, but it at least won't crash when running on earlier versions.

这篇关于不使用 renderInContext 捕获 UIView:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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