场景
-
在
Cocoa
开发时, 界面上需要显示动态的GIF
动画, 可以试用NSImageView
, 但是如果在NSTableView
的某个单元格显示GIF
动画, 如何做? -
NSTableView
的某列如果使用NSImageCell
时, 会有什么问题?
说明
-
在实验中, 如果
NSTableView
,内容模式是Cell Based
只是在某时只有一行显示GIF
动画, 使用NSImageCell
,并且在IB
里勾选Animates
时, 并实现NSTimer
定时器来定时刷新这个单元格, 是可以实现GIF
的动态效果的. 但是如果是多行同时显示, 很奇怪,GIF
动画就不动了, 但是NSTimer
仍然是刷新的. 初步判断是获取NSImage
的当前NSImageCurrentFrame
出问题, 在多行同时显示GIF
时,NSImageCurrentFrame
的值在执行过一轮之后值没变, 导致图片看起来没动. 所以这时候如果能手动设置NSImageCurrentFrame
值,应该就能解决问题. 事实上的确可以解决问题. -
注意,
IB
里面NSImageCell
属性Animates
不能勾选, 不然会有冲突.
-(void) onTimer:(NSTimer*) data
{
[ImageUtil sepatateGifAnimation:image_loading_ withFrameCount:&frame_count_];
[list_view_ reloadDataForRowIndexes:[NSIndexSet indexSetWithIndex:row] columnIndexes:[NSIndexSet indexSetWithIndex:2]];
}
+(void) sepatateGifAnimation:(NSImage *) gifImage withFrameCount:(int*)frame_count;
{
NSArray * reps = [gifImage representations];
for (NSImageRep * rep in reps){
// find the bitmap representation
if ([rep isKindOfClass:[NSBitmapImageRep class]] == NO)
continue;
// get the bitmap representation
NSBitmapImageRep * bitmapRep = (NSBitmapImageRep *)rep;
// check multiframe gif
int numFrame = [[bitmapRep valueForProperty:NSImageFrameCount] intValue];
if (numFrame == 0)
break;
[bitmapRep setProperty:NSImageCurrentFrame withValue:[NSNumber numberWithInt:*frame_count]];
if(++(*frame_count) == numFrame)
*frame_count = 0;
}
}