How to animate a GIF image in Cocoa and set it to not loop.

3.4k views Asked by At

Just as my title mentions, I want to use NSImageView, To "animate" an image, but not loop when it reaches the end of its animation.

2

There are 2 answers

2
Heinrich Giesen On BEST ANSWER

The infinite loop of a gif image is not a bad habit of the NSImageView, it is determined by a value (in Application Extension) within the gif image data. You can ask for that value by

NSBitmapImageRep *gifRep = // get the gif image from wherever
NSInteger loopCount = [[gifRep valueForProperty: NSImageLoopCount] integerValue];
// a lopCount of zero means: infinite

But you can also set that value (before sending it to the NSImageView). If you want the animation to stop after one loop, do this:

NSImage *img = [[NSImage alloc] initWithContentsOfFile:fileName]; // or similar
NSBitmapImageRep *gifRep = [[img representations] objectAtIndex:0];
[gifRep setProperty:NSImageLoopCount withValue:@(1)]; //one loop, then stop
[myImageView setImage:img];
0
Swati On

Finally, I achieved Gif animation in NSImageView.

Xcode 8.2 OSX 10.12

 self.imageView.imageScaling = NSImageScaleProportionallyDown;
 self.imageView.animates = YES;
 self.imageView.canDrawSubviewsIntoLayer = YES;
 [self.imageView setImage:[NSImage imageNamed:@"play.gif"]];