我想从我的屏幕创建视频,为此我将捕获视图,但我不想使用renderInContext:
由于某种原因。我现在正在使用drawViewHierarchyInRect:
,但这仅限于 iOS 7,我的应用程序也支持早期的 iOS 版本。我会因使用而被罚款吗drawViewHierarchyInRect:
?
这是我的代码
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
我明白你不想使用renderInContext
因为它效率较低,但对于 7 之前的 iOS 版本,这是您应该使用的技术(因为如果您尝试使用drawViewHierarchyInRect
在 7.0 之前的 iOS 版本中,应用程序会崩溃)。所以,这是一个使用的再现drawViewHierarchyInRect
当它可用时,但是renderInContext
当没有时:
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+ 更高效的机制,但在早期版本上运行时至少不会崩溃。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)