我已将动画 GIF 成功加载到 NSData
或 NSBitmapImageRep
对象中。 Reference for NSBitmapImageRep
我已经想出了如何使用以下方法返回数据,例如该 gif 中的帧数:
NSNumber *frames = [bitmapRep valueForProperty:@"NSImageFrameCount"];
但是,对于如何实际访问该框架作为它自己的对象,我有点困惑。
我认为这两种方法中的一种会有所帮助,但我实际上不确定他们将如何为我获得单独的框架。
+ representationOfImageRepsInArray:usingType:properties:
– representationUsingType:properties:
感谢任何帮助。谢谢
最佳答案
I've figured how to return data like the number of frames in that gif using:
NSNumber *frames = [bitmapRep valueForProperty:@"NSImageFrameCount"];
However, I'm a bit confused as to how I can actually access that frame as its own object.
访问特殊框架indexOfFrame
( 0 <= indexOfFrame < [frames intValue]
) 你只需要设置 NSImageCurrentFrame
你完成了。无需使用 CG 功能或复制帧。您可以留在面向对象的 Cocoa 世界中。一个小例子显示了所有 GIF 帧的持续时间:
NSNumber *frames = [bitmapRep valueForProperty:@"NSImageFrameCount"];
if( frames!=nil ){ // bitmapRep is a Gif imageRep
for( NSUInteger i=0; i<[frames intValue]; i++ ){
[bitmapRep setProperty:NSImageCurrentFrame
withValue:[NSNumber numberWithUnsignedInt:i] ];
NSLog(@"%2d duration=%@",
i, [bitmapRep valueForProperty:NSImageCurrentFrameDuration] );
}
}
另一个例子:将 GIF 图像的所有帧作为 PNG 文件写入文件系统:
NSNumber *frames = [bitmapRep valueForProperty:@"NSImageFrameCount"];
if( frames!=nil ){ // bitmapRep is a Gif imageRep
for( NSUInteger i=0; i<[frames intValue]; i++ ){
[bitmapRep setProperty:NSImageCurrentFrame
withValue:[NSNumber numberWithUnsignedInt:i] ];
NSData *repData = [bitmapRep representationUsingType:NSPNGFileType
properties:nil];
[repData writeToFile:
[NSString stringWithFormat:@"/tmp/gif_%02d.png", i ] atomically:YES];
}
}
关于objective-c - 如何访问动画 GIF 的帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17161487/