drawViewHierarchyInRect vs renderInContext

I came across very curious problem whose cause I couldn’t have come to know about it without serious debugging because this was something that didn’t catch my attention. I had a task that required capturing snapshot of UIView with hierarchy of subviews and perform few interactive transformation animations on view immediately after - giving feeling of instant multitask. Delay should have been negligent for naked eyes or tolerant but it wasn’t.

I used drawViewHierarchyInRect -

[viewport drawViewHierarchyInRect:viewport.bounds afterScreenUpdates:YES];

Then I performed animation. Instructions written were executed well except the long delay between them. Delay was 3 seconds before my animations kicked in. Interactive Animations were running on main thread. I tinkered with my transformation code for hours to no avail. I noticed something where animations were very smooth and responsive when they actually ran. I then decided maybe delay problem could be with snapshot capture. Commenting the snapshot capturing code would bring up my animations quick.

It seemed drawViewHierarchyInRect would block the thread running my animations until snapshot capture was ready. I am not saying if it really blocked but visually gave feeling that it was blocking the animation for certain delay. Even so why so much delay? Capture was instant. I did test just capture to see how long does it takes to get the captured snapshot of UIView and it was very quick about few milliseconds. Visually it was instant. The documentation says this method is slow albeit but do work well for capturing graphical effects and transformations. Could this be the reason - the mechanism with which it captures snapshot of details and effects? I can’t say.

I changed the code to capture the snapshot of UIView state. I used renderInContext -

[viewport.layer renderInContext:UIGraphicsGetCurrentContext()];

This worked. There was no feeling of thread blocking. Everything worked together in harmony. Document says it ignores animations so I assume it ignores some of transformations too. My UIView had no graphics effect like blur or any complex transformation that method may have unsupported. In the end this worked for me.