11

I'm creating small tile images to be stored on disk and displayed to the user. Here is my current process for doing this:

  1. Create a viewcontroller that represents the UI I want to show onscreen
  2. Get the view from the viewcontroller and render an image from it
  3. Save it to disk and display it onscreen later

I am crashing when I attempt to access the view of the viewcontroller. When I tried to research this online, I am getting conflicting results on whether it is safe to create the view in the background. I'm reading that the UIGraphicsGetCurrentContext call should be thread safe, but maybe not accessing the UIView on a background thread? I'm writing the app for iOS 4 and above. Here is the code I'm using (tile is the viewcontroller):

CGSize size = CGSizeMake(20.0f, 30.0f);
UIGraphicsBeginImageContext(size);
[tile.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();           
UIGraphicsEndImageContext();

The crash occurs when trying to access the .view property on the tile (EXC_BAD_ACCESS). The whole point is to render the view to an image in the background to prevent locking up the UI because there are a lot of tiles to process.

Is there a safe way to do this?

Mark Struzinski
  • 32,945
  • 35
  • 107
  • 137
  • 1
    Did you ever find a good solution to this? – Ben Williams Nov 01 '12 at 06:18
  • I ran into the same crash. Based on [Apple's documentation](http://developer.apple.com/library/ios/#qa/qa1714/_index.html), `renderInContext` CAN BE used on a different thread. However, in reality, it crashes mysteriously. Did anyone find a solution to this problem? – Howy Jul 16 '13 at 20:54
  • Did you find a solution for this, Mark? – Roi Mulia Sep 22 '17 at 11:16

1 Answers1

2

I've never tried what you described, and I don't like being the bearer of bad news, but taken from Apple's documentation.

Note: For the most part, UIKit classes should be used only from an application’s main thread. This is particularly true for classes derived from UIResponder or that involve manipulating your application’s user interface in any way.

Of course, they conveniently say "For the most part", so that leaves some wiggle room.

Pieter
  • 17,435
  • 8
  • 50
  • 89
  • What about passing a view without manipulating it? I'm not actually dealing with UI on screen at this point. I'm just creating a view and saving it to an image. If I'm not placing it onscreen, would there be a safe way to handle this? – Mark Struzinski Jun 14 '11 at 20:56
  • Writing the image to disk in a separate thread can certainly be done in a safe way. Getting the image from the calayer however... no idea to be honest. I suppose it all depends on what happens inside `renderInContext`, but that kind of info is not documented anywhere as far as I know. – Pieter Jun 15 '11 at 08:39
  • Thanks, Pieter. I'm going to try to find a workaround for our methodology. – Mark Struzinski Jun 16 '11 at 13:43
  • 8
    @MarkStruzinski Did you ever come up with a solid workaround for rendering layers to images in the background without tying up the UI? – coneybeare Nov 20 '11 at 03:53