UIImage setImage is very, very slow

I am trying to download an image from a local url using the following:

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:fileURL]]; [self.imageView setImage:image]; NSLog(@"imageView set"); 

So, I see the "imageView set" on the console almost immediately, but it takes a very long time for it to be reflected in the user interface (sometimes a few minutes!).

Any idea why this is happening?

+7
source share
4 answers

This happened to me when I installed the image in the background thread (in which I downloaded the image file). Just make sure your code is running in the main thread. The image changes immediately.

 // In a background thread... UIImage *image = ... // Build or download the image dispatch_async(dispatch_get_main_queue(), ^{ [self.imageView setImage:image]; // Run in main thread (UI thread) }); 
+10
source

You must download the tools and see what exactly he does.

First, you must do everything possible to avoid the input / output in the drawing stream. Also, are there other I / O requests at the same time?

A UIImage does not have to be a unique representation of a bitmap - it can be backed up by a cache and / or loaded lazily. Therefore, simply because the “image” is specified does not mean that the optimal raster map has been loaded into memory and prepared for rendering - it can be delayed until rendering (drawing) is requested.

OTOH profiling will tell you (usually) why it takes longer than expected.

+4
source

Downloading an image from NSData takes longer. Instead, you can simply use the following code:

 UIImage *image = [UIImage imageWithContentsOfFile:fileURL]; 

Give it a try.

-one
source

So, I tried the following:

 dispatch_async(dispatch_get_global_queue(0, 0), ^{ UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:fileURL]]; [self.imageView setImage:image]; NSLog(@"imageView set"); }); 

And it's a little faster. Not sure why.

-2
source

All Articles