Animated GIFs received as NSData via NSSocket, unable to split frames

I'm having issues with how iOS handles animated GIFs. I know that you cannot use animated GIFs in UIImageView, and you must use custom animations in UIImageView.

But..

I have a Java server that sends GIF images over a socket. IOS (iPhone) receives this stream and converts it to an NSData type. I managed to capture and display this image in UIImageView, but, as many of you already know, it displays only the first frame.

I also found some code to decode this GIF into separate images, but this code works from a GIF file, not NSData.

Question: How to convert an NSData file to separate images and put them in an NSArray to use as an animation?

Note. The resulting NSData has an image and some text separated by a rare character. therefore, NSData looks like this: [image] [separator] [text].

Hope someone can give me some pointers or some samples to work with.

Thanks in advance, I will continue the search until you or I find the answer :)

+8
ios objective-c iphone sockets uiimageview
source share
2 answers

If you do not target devices before iOS 4, use ImageIO.

NSMutableArray *frames = nil; CGImageSourceRef src = CGImageSourceCreateWithData((CFDataRef)data, NULL); if (src) { size_t l = CGImageSourceGetCount(src); frames = [NSMutableArray arrayWithCapacity:l]; for (size_t i = 0; i < l; i++) { CGImageRef img = CGImageSourceCreateImageAtIndex(src, i, NULL); if (img) { [frames addObject:[UIImage imageWithCGImage:img]]; CGImageRelease(img); } } CFRelease(src); } 
+14
source share

I made a wrapper that also handles animation time based on code from Anomie

https://gist.github.com/3894888

+2
source share

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


All Articles