# Displaying animated gifs in Cocoa



## Mikuro (Nov 16, 2005)

I have two questions regarding using NSImageView to display animated gifs:

1. As in Safari, playback is very slow until it loops around the first time. I can only assume that it doesn't load each frame into RAM until the first time it displays it. Is there any way to fix this so it plays smoothly the _first_ time through? This has always bothered me to no end with Safari, and I _really_ don't want my programs to have the same problem....

2. Some gifs won't animate at all. Is seems like all gifs that do not specify an interframe delay (which means they should play as fast as possible) won't work in NSImageView. They DO, however, play in Safari. Any idea what I need to do to get it working?


----------



## kainjow (Nov 16, 2005)

I don't get the question. Are you trying to display an animated gif in your program, or in a web page? If you want the best playback, don't use NSImageView, use QuickTime to play it back (i.e. QTKit).


----------



## Mikuro (Nov 16, 2005)

In my program. I'm just mentioning Safari's behavior for reference.

I suppose I could use QuickTime, but it would take an annoyingly large rewrite of my program to use both NSImageView (for regular images) and QTMovieView for animated gifs. Surely there's some way to get decent performance in Cocoa....right?

In any case, thanks for the reply. I'll start looking into QT.


----------



## kainjow (Nov 16, 2005)

Well you could do it all manually in an NSImageView, but that'd be kind of a pain: http://www.cocoabuilder.com/archive/message/cocoa/2004/1/29/99773


----------



## lurk (Nov 16, 2005)

In Safari are you sure that the whole gif has been downloaded form the network?  If all the frames are not available you could be seeing the network delay.


----------



## Mikuro (Nov 16, 2005)

lurk said:
			
		

> In Safari are you sure that the whole gif has been downloaded form the network?  If all the frames are not available you could be seeing the network delay.


The delay in Safari is exactly the same as the delay in my own Cocoa apps, and it happens for local files, too. The only difference I can see between the standard NSImageView implementation and Safari is that Safari can play ALL animated gifs, even the ones with no interframe delay specified, which NSImageView doesn't seem to know how to handle. This leads me to believe that I should be able to get those gifs playing in NSImageView, too  I just have no idea how.



			
				kainjow said:
			
		

> Well you could do it all manually in an NSImageView, but that'd be kind of a pain: http://www.cocoabuilder.com/archive/message/cocoa/2004/1/29/99773


I was just doing some experiments trying to preload the gif for NSImageView. I used this code:
	
	



```
//load a gif into image here...
NSImageRep *r = [[image representations] objectAtIndex:0];
if ([r isMemberOfClass:[NSBitmapImageRep class]]) {
	NSNumber *n=[(NSBitmapImageRep*)r valueForProperty:NSImageFrameCount];
	if ((n) && [n intValue]>1) {
		int i;
		for (i=0; i<[n intValue]; i++) {
			[(NSBitmapImageRep*)r setProperty:NSImageCurrentFrame withValue:[NSNumber numberWithInt:i]];
		}
	}
}
```
The good part is that after doing that, the gif plays at full speed the first time through. The bad part is that routine takes every bit as long as NSImageView normally takes to play through the gif the first time, so this method is really much _worse_ than just giving it straight to the NSImageView. It also means that there's no point in trying to write my own ImageView to work with these BitmapImageReps, since it'd be just as slow as NSImageView.

Oh well. Looks like QuickTime really is the only good option.

Now that we're on the topic of QuickTime, I have one more question: Should I use NSMovieView or QTMovieView? I've noticed that QTMovieView delivers the same fast resizing seen in QuickTime Player 7, whereas NSMovieView does not. This doesn't really matter in my case, but I found it interesting. Are there any other differences between the two?

I'd still be interested in a way to get those other gifs to play, though, even if there's no way to resolve the performance issue.

Thanks for all the replies.


----------



## kainjow (Nov 16, 2005)

I'd use QT if the QT 7 requirement doesn't bother you. NSMovieView pretty much sucks


----------

