NSBitmapImageRep来自动画GIF的持续时间

时间:2014-11-23 20:39:04

标签: objective-c macos cocoa

我有两个GIF动画:
http://d-32.com/uploads/gif1.gifhttp://d-32.com/uploads/gif2.gif

使用NSBitmapImageRep时,第一个gif的帧持续时间为0.15s,第二个gif的帧持续时间为0.1s。 但是对于第二个gif来说,0.1是减速的方法。

使用imagemagick时,我的第一个获得0.15s,第二个gif获得0.03s,这是正确的。

我做错了什么,或者是NSBitmapImageRep,即使用NSImageView显示gifs无用,我将不得不诉诸webview(可以正确显示两个GIF)?

1 个答案:

答案 0 :(得分:0)

非常感谢Heinrich,我现在知道gif是两个值,真实值和钳位值。可悲的是NSBitmapImageRep只提供了限制值,但是使用CGImageProperty我可以读取这两个值。所以我遍历每一帧,用正确的帧更新持续时间,最后创建一个CAAnimation,如:https://github.com/orta/GIFs/blob/master/objc/GIFs/ORImageView.m

所以这是更新持续时间的代码:

NSDictionary *properties = (__bridge_transfer NSDictionary *)CGImageSourceCopyPropertiesAtIndex(imageSource, i, nil);
float duration = [[[properties objectForKey:(__bridge NSString *)kCGImagePropertyGIFDictionary]
                   objectForKey:(__bridge NSString *) kCGImagePropertyGIFUnclampedDelayTime] doubleValue];
[bitmapRepresentation setProperty:NSImageCurrentFrame withValue:[NSNumber numberWithInt:i]];
[bitmapRepresentation setProperty:NSImageCurrentFrameDuration withValue:[NSNumber numberWithFloat:duration]];
相关问题