在不使用renderInContext的情况下捕获UIView:

我想从屏幕上创buildvideo,为了捕获视图,我不想使用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版本,这是你应该使用的技术(因为如果你试图在7.0版本以前的iOS版本中使用drawViewHierarchyInRect ,应用程序将会崩溃)。 所以,这是一个当可用时使用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+机制,但在早期版本上运行时至less不会崩溃。