Safe way to display UIVIew on an image in a background thread?

I create small fragments of an image that will be stored on disk and displayed to the user. Here is my current process for doing this:

  • Create a view manager representing the user interface I want to show on the screen
  • Get view from view manager and display image from it
  • Save it to disk and display it later.

I am trying to access viewcontroller view. When I tried to explore this online resource, I get conflicting results about whether it is safe to create a view in the background. I read that calling UIGraphicsGetCurrentContext should be thread safe, but maybe not access the UIView in the background thread? I am writing an application for iOS 4 and above. Here is the code I'm using (tile - 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 thing is to render the image in the background in order to prevent the user interface from being blocked, since processing requires a lot of fragments.

Is there a safe way to do this?

+6
source share
1 answer

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

Note. For the most part, UIKit classes should only be used with the main application thread. This is especially for classes derived from UIResponder or user manipulation of the application interface anyway.

Of course, they conveniently say "For the most part," so it leaves some room for maneuver.

+2
source

Source: https://habr.com/ru/post/890535/


All Articles