[Cocoa]_[中级]_[表格里显示GIF动画问题]

Cocoa中GIF动画显示解决方案

场景

  1. Cocoa开发时, 界面上需要显示动态的GIF动画, 可以试用NSImageView, 但是如果在NSTableView的某个单元格显示GIF动画, 如何做?

  2. NSTableView的某列如果使用NSImageCell时, 会有什么问题?

说明

  1. 在实验中, 如果NSTableView,内容模式是Cell Based只是在某时只有一行显示GIF动画, 使用NSImageCell,并且在IB里勾选Animates时, 并实现NSTimer定时器来定时刷新这个单元格, 是可以实现GIF的动态效果的. 但是如果是多行同时显示, 很奇怪, GIF动画就不动了, 但是NSTimer仍然是刷新的. 初步判断是获取NSImage的当前NSImageCurrentFrame出问题, 在多行同时显示GIF时, NSImageCurrentFrame的值在执行过一轮之后值没变, 导致图片看起来没动. 所以这时候如果能手动设置NSImageCurrentFrame值,应该就能解决问题. 事实上的确可以解决问题.

  2. 注意, 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;
    }
}

参考

Cocoa中对GIF动画分解成一帧帧的图片

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白行峰 (花名)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值