2

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:?

this is my code

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

1 Answers1

3

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();

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

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • It works but unfortunately create break in **CFDataGetBytes(image, CFRangeMake(0, CFDataGetLength(image)), destPixels);** which I've use to write image into video – Altaf Bangash Apr 21 '14 at 13:16
  • That sounds like a very different issue. When I want to get pixel data from a `UIImage`, I get the `CGImage` for the `UIImage` and then use the technique that Apple discusses in [QA1509](https://developer.apple.com/library/mac/qa/qa1509/_index.html). See http://stackoverflow.com/a/22897776/1271826 for an example. – Rob Apr 21 '14 at 13:23