因此,感谢海因里希,我现在知道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]];
你这样做没有错。 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
这只是一个示例gifs,我将不得不处理未知的gif,因此硬编码0.03将不起作用。所以基本上我必须自己读取gif文件中的信息? – 2014-11-24 16:31:06