2014-11-23 108 views
0

我有两个动画GIF:
http://d-32.com/uploads/gif1.gifhttp://d-32.com/uploads/gif2.gifNSBitmapImageRep从持续时间动画GIF

当使用NSBitmapImageRep我得到的0.15s用于第二第一GIF和0.1s的帧持续时间。
但0.1是减慢第二个gif的方法。

当使用imagemagick我也得到0.15s为第一,但0.03s为第二个gif,这是正确的。

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

+0

你这样做没有错。 NSBitmapImageRep使用DelayTime(第二个gif中的0.1秒)而不是UnclampedDelayTime(0.03秒)。这两个值都显示在预览应用程序和相应的CGImage中(每个NSBitmapImageRep都是一个CGImage)。但NSBitmapImageRep使用了钳位值,因为它认为0.03秒太快(而你认为0.1秒太慢)。应该可以使用setProperty为NSBitmapImageRep中的每个帧更改这些值:NSImageCurrentFrameDuration withValue:@ 0.03但是我的第一个实验失败了,仍在继续。 – 2014-11-24 16:29:07

+0

这只是一个示例gifs,我将不得不处理未知的gif,因此硬编码0.03将不起作用。所以基本上我必须自己读取gif文件中的信息? – 2014-11-24 16:31:06

回答

0

因此,感谢海因里希,我现在知道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]];