drawViewHierarchyInRect sometimes returns transparent image

Originator:ash.furrow
Number:rdar://18525028 Date Originated:October 2, 2014
Status:Open Resolved:
Product:Developer tools Product Version:6.1/6.0.1
Classification:Other Bug Reproducible:Sometimes
 
Summary:
So here's a tricky one. Taken a while to track down, but essentially, drawViewHierarchyInRect will sometimes return a UIImage instance of the correct size but missing the contents of the view, at least in a unit testing target.

Steps to Reproduce:
- Create a new app project (iOS)
- Got to the unit tests created 
- Now create a simple test that generates a UIView instance, renders it to a UIImage, and turns that image into a PNG:

let sut = UIView(frame: CGRectMake(0, 0, 100, 100))
sut.backgroundColor = UIColor.purpleColor()

UIGraphicsBeginImageContextWithOptions(sut.frame.size, false, 0)
sut.drawViewHierarchyInRect(sut.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()

let data = UIImagePNGRepresentation(image)

println("byte length: \(data.length)")

- Easy, and this will likely work. Now repeat that exact same code in three different test methods.

func testExample1() { ... }
func testExample2() { ... }
func testExample3() { ... }

Then you'll get the following logging:

Test Suite 'All tests' started at 2014-10-02 12:26:18 +0000
Test Suite 'TestTestTests.xctest' started at 2014-10-02 12:26:18 +0000
Test Suite 'TestTestTests' started at 2014-10-02 12:26:18 +0000
Test Case '-[TestTestTests.TestTestTests testExample1]' started.
byte length: 1411
Test Case '-[TestTestTests.TestTestTests testExample1]' passed (0.175 seconds).
Test Case '-[TestTestTests.TestTestTests testExample2]' started.
byte length: 1411
Test Case '-[TestTestTests.TestTestTests testExample2]' passed (0.017 seconds).
Test Case '-[TestTestTests.TestTestTests testExample3]' started.
byte length: 887
Test Case '-[TestTestTests.TestTestTests testExample3]' passed (0.002 seconds).
Test Suite 'TestTestTests' passed at 2014-10-02 12:26:19 +0000.
	 Executed 3 tests, with 0 failures (0 unexpected) in 0.194 (0.195) seconds
Test Suite 'TestTestTests.xctest' passed at 2014-10-02 12:26:19 +0000.
	 Executed 3 tests, with 0 failures (0 unexpected) in 0.194 (0.196) seconds
Test Suite 'All tests' passed at 2014-10-02 12:26:19 +0000.
	 Executed 3 tests, with 0 failures (0 unexpected) in 0.194 (0.197) seconds

The byte lengths are inconsistent, and if you pause the debugger to quick look the image, you'll see it's empty (usually in the second or third test) – see attached screenshot.

Expected Results:
Views rendered with drawViewHierarchyInRect should always return a valid snapshot of that view, not a transparent image of the correct size.

Actual Results:
Frequently, though intermittently, this API fails. 

Version:
Verified on Xcode 6.0.1 and 6.1 beta 3

Attachments:
Sample project: http://cloud.ashfurrow.com/2D1d28151R47
Screenshot: http://cloud.ashfurrow.com/image/0Y0h1F2I3727

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!