Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS7's drawViewHierarchyInRect doesn't work?

From what I've read, iOS7's new drawViewHierarchyInRect is supposed to be faster than CALayer's renderInContext. And according to this and this, it should be a simple matter of calling:

[myView drawViewHierarchyInRect:myView.frame afterScreenUpdates:YES];

instead of

[myView.layer renderInContext:UIGraphicsGetCurrentContext()];

However, when I try this, I just get blank images. Full code that does the capture, where "self" is a subclass of UIView,

        // YES = opaque. Ignores alpha channel, so less memory is used.
        // This method for some reasons renders the
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, self.window.screen.scale);    // Still slow.

    if ( [AIMAppDelegate isOniOS7OrNewer] )
        [self drawViewHierarchyInRect:self.frame afterScreenUpdates:YES]; // Doesn't work!
    else
        [self.layer renderInContext:UIGraphicsGetCurrentContext()];     // Works!

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

    contentImageView.image = image; // this is empty if done using iOS7's way

and contentImageView is a UIImageView that is added as a subView to self during initialization.

Additionally, the drawing that I want captured in the image is contained in other sub-views that are also added to self as a sub-view during initialization (including contentImageView).

Any ideas why this is failing when using drawViewHierarchyInRect?

* Update *

I get an image if I draw a specific sub-view, such as:

[contentImageView drawViewHierarchyInRect:contentImageView.frame afterScreenUpdates:YES];

or

[self.curvesView drawViewHierarchyInRect:self.curvesView.frame afterScreenUpdates:YES];

however I need all the visible sub-views combined into one image.

like image 433
Vern Jensen Avatar asked Nov 06 '13 20:11

Vern Jensen


1 Answers

Try it with self.bounds rather than self.frame—it’s possible you’re getting an image of your view rendered outside the boundaries of the image context you’ve created.

like image 123
Noah Witherspoon Avatar answered Nov 15 '22 09:11

Noah Witherspoon