Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid image decompression blocking the main thread

I have some code to display an animated GIF image using a UIImageView, here: https://github.com/amleszk/GifBlocking

It works well for 99% of cases, although there is an issue with a certain type of GIF image, An example can be found here: http://i.imgur.com/mbImw.gif

This gif receives 101 images fine, then blocks the main thread when it comes time to display the UIImageView containing the animated images. Its fine to decompress the gif if it has compression, but how would i stop this blocking the main thread?

The methods that get invoked on the main thread are DGifDecompressInput DGifDecompressLine copyImageBlockSetGIF

the problem is the gif decompression happens when the view gets added to the heirarchy - which should be done on the main thread

Thanks

like image 645
amleszk Avatar asked Apr 09 '26 00:04

amleszk


2 Answers

grasGendarme's code is useful, but note that UIImage is lazy and won't decode the image until it is really needed. The key is that you have to force decompression on a background thread using CGContextDrawImage. So use UIImage+JTImageDecode.h to create an uncompressed image version on the background, then set it back to the main thread.

like image 82
Jano Avatar answered Apr 11 '26 19:04

Jano


It would be great to see the actual code. Without that, our help is limited.

Maybe you can put a line:

[self performSelectorInBackground:@selector(yourBlockingMethod:) withObject:yourObject];

Or modify your library to decompress the GIF on a background thread, then use setNeedsDisplay on the main thread.

like image 27
Ilea Cristian Avatar answered Apr 11 '26 19:04

Ilea Cristian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!