objective-c - 如何访问动画 GIF 的帧

标签 objective-c macos cocoa core-graphics core-image

我已将动画 GIF 成功加载到 NSDataNSBitmapImageRep 对象中。 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/

相关文章:

ios - 我可以只对特定的单元格标识符执行 didSelectRowAtIndexPath 吗?

objective-c - 阻止窗口打开(无窗口应用程序)

cocoa - 无法在支持 AppleScript 的 Core-data 应用程序中使用来自“新建”命令的引用

objective-c - 苹果操作系统 X : Intercept keyboard layout change

objective-c - Objective C ==> unichar[] 到 NSString?

iOS:将手势传递给下面的 View

iphone - 如何测试 NSString 是否为 nil?

macos - 如何修复 Homebrew 权限?

objective-c - XPC服务支持多种协议(protocol)

objective-c - 防止 NSTimer 在后台应用程序中触发延迟