如何在Cocoa中为GIF图像设置动画并将其设置为不循环.

And*_*ang 1 macos cocoa gif nsimageview

就像我的标题所提到的那样,我想使用NSImageView来"动画"图像,但是当它到达动画结束时不会循环.

小智 7

gif图像的无限循环不是NSImageView的坏习惯,它由gif图像数据中的值(在应用程序扩展中)确定.您可以通过询问该值

NSBitmapImageRep *gifRep = // get the gif image from wherever
NSInteger loopCount = [[gifRep valueForProperty: NSImageLoopCount] integerValue];
// a lopCount of zero means: infinite
Run Code Online (Sandbox Code Playgroud)

但您也可以设置该值(在发送之前NSImageView).如果您希望动画在一个循环后停止,请执行以下操作:

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];
Run Code Online (Sandbox Code Playgroud)