TchK
TchK

Reputation: 11

UIScrollView animation laggy

I'm writing an reader for iPad that shows images (1024x1322 max) in a UIScrollView

each image is loaded in a background thread when it's needed (during scroll) (there is always 3 images loaded at max).

My problem is: when the data is loaded I'm updating the UIImageView, in my background thread like that:

image= [[UIImage alloc] initWithContentsOfFile: file];
[content setImage:image]

It seems to 'lock' the main thread and make the UISCrollView animation not smooth at all.

Thank you in advance for any clue

Upvotes: 1

Views: 1038

Answers (2)

omz
omz

Reputation: 53551

You should never set the image of an image view (or any property of any UI component for that matter) from a background thread. UIKit is not thread-safe. You need to pass the image to the main thread, using either GCD or performSelectorOnMainThread:withObject:waitUntilDone:.

Upvotes: 2

Nick Hingston
Nick Hingston

Reputation: 8880

do something like this in your background thread, instead of using UIImage

CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename("file.png"); 
CGImageRef image = CGImageCreateWithPNGDataProvider(dataProvider, NULL, NO, kCGRenderingIntentDefault);

pass back the image ref to your main thread then create the UIImage with [UIImage imageWithCGImage:image]

Hope this helps

Upvotes: 0

Related Questions