Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animated-GIF images received as NSData through NSSocket unable to split frames

I'm having trouble with the way iOS handles animated-GIF's. I know that you cannot use animated-GIF's on an UIImageView and you have to use custom animations on the UIImageView.

But..

I have a Java server that sends GIF images through a socketstream. The iOS (iPhone) receives that stream and converts it into an NSData type. I've succeeded in capturing and displaying this image in a UIImageView but as many of you already know.. it only displays the first frame.

I Also found some code to decode that GIF into separate images, but that code works from a GIF file and not NSData.

Question: How do i convert the NSData file into separate images and place them in an NSArray to use it as animation?

Note: In the NSData that is received are both an image and some text separated by a rare character. so the NSData looks like this: [image] [separator] [text].

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

Thanks in advance, i will keep searching untill you or me finds an answer :)

like image 692
stackr Avatar asked Dec 15 '10 13:12

stackr


2 Answers

Unless you're targeting 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);
}
like image 171
Anomie Avatar answered Sep 30 '22 15:09

Anomie


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

https://gist.github.com/3894888

like image 44
Andrei Puni Avatar answered Sep 30 '22 15:09

Andrei Puni