2

I have been using the following code in my application to render out a custom UIView to an image. The code works great in the Simulator (iPad and iPad retina) as well as on iPad 1 and iPad 2 devices. However, I recently tested it on the iPad 3 device, and am getting a crash that I can't seem to resolve. The relevant code snippet is:

UIGraphicsBeginImageContext(CGSizeMake(myUIView.frame.size.width, myUIView.frame.size.height));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
[myUIView.layer renderInContext:context];

The application crashes on the last line. The following is in the crash log:

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x12311000
Crashed Thread:  5

And from later in the log:

Thread 5 name:  Dispatch queue: com.apple.root.default-overcommit-priority
Thread 5 Crashed:
0   ImageIO                         0x3384bcb4 ImageIO_ABGR_TO_ARGB_8Bit + 68
1   ImageIO                         0x3388761c __copyImageBlockSetPNG_block_invoke_1 +  608
2   libdispatch.dylib               0x348c0c52 _dispatch_call_block_and_release + 6
3   libdispatch.dylib               0x348c3810 _dispatch_worker_thread2 + 252
4   libsystem_c.dylib               0x33145df4 _pthread_wqthread + 288
5   libsystem_c.dylib               0x33145cc8 start_wqthread + 0

I first thought that this was a memory management error, but it's only on the iPad 3 device and I'm using ARC. This has me stumped.

Any suggestions of where I might look to troubleshoot this further? Has anyone else encountered this sort of behavior?

Thanks in advance!

Benchtop Creative
  • 583
  • 1
  • 5
  • 13
  • HI,Have you found a solution for this,im also having same problem ? If so pls share the solution – Infaz Dec 20 '15 at 17:38

2 Answers2

2

Try

UIGraphicsBeginImageContextWithOptions(CGSizeMake(myUIView.frame.size.width, myUIView.frame.size.height), NO, [[UIScreen mainScreen] scale]);
lorean
  • 2,150
  • 19
  • 25
  • I was thinking along these lines too, and tried this:UIGraphicsBeginImageContextWithOptions(CGSizeMake(myUIView.frame.size.width, myUIView.frame.size.height), YES, 0.0); Unfortunately the crash behavior is the same – Benchtop Creative Jun 28 '12 at 23:09
  • I tried with your proposed options variant and that also triggers the same crash – Benchtop Creative Jun 28 '12 at 23:12
  • Sad kittens. I wonder if the layer could be mutated during the rendering operation. The call stack suggests you're rendering from a background thread (i.e Thread 5). Hmm. Try moving it on the main thread to see if that fixes the problem? Then try to (serially) make a copy of the CALayer then pass it to a background thread. – lorean Jun 28 '12 at 23:22
  • I did try making a copy by inserting this snippet: CALayer *myLayer = myUIView.layer; [myLayer renderInContext:context]; - but the same result. I'm not sure how to move it to the main thread. Is this just a matter of where I call the function from? – Benchtop Creative Jun 28 '12 at 23:33
1

Was finally able to track down the source of the crash to a particular UIImageView in the view that I was trying to render. This view was using a 2x graphic for iPad 3 retina. While it displayed just fine, it apparently proved to consume too much memory during the renderInContext call. I reduced the size of the graphic and also reduced the rendering scale in the context options. This resolved the crash.

Benchtop Creative
  • 583
  • 1
  • 5
  • 13
  • 2
    Even if the reduced image size helped, it is not safe to call -renderInContext: on a layer backing a UIKit class from a background thread. More likely is that the retina image took long enough to render that you ran into contention with UIKit on the main thread. – Jason Coco Jul 03 '12 at 04:53
  • I'm not intentionally calling this on a separate thread. I'm rendering custom UIView that is animated in via an animation block. Can you recommend a means to put it on the main thread? Is it on a background thread because of the animation? Some other reason that I can troubleshoot? Thanks in advance! – Benchtop Creative Jul 03 '12 at 05:17
  • It is called in response to a button press, but yes, I haven't in any explicit way spawned a background thread. – Benchtop Creative Jul 03 '12 at 05:22
  • Ah, ok, then it's not what I suggested! Based on the snippet and the crash it looked like you were rasterizing the view on a background thread. – Jason Coco Jul 03 '12 at 05:23