ios - `Gif`动画内存暴增!
大家讲道理
大家讲道理 2017-04-17 17:45:24
0
2
284

最近要展示一个gif图,大小是274KB750*1334

@implementation GifView
{
    CGImageSourceRef gif;
    NSDictionary *gifProperties;
    size_t index;
    size_t count;
    NSTimer *timer;
}

- (id)initWithFrame:(CGRect)frame data:(NSData *)_data{
    self = [super initWithFrame:frame];
    if (self) {
        gifProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount]
                                                     forKey:(NSString *)kCGImagePropertyGIFDictionary];
        gif = CGImageSourceCreateWithData((CFDataRef)_data, (CFDictionaryRef)gifProperties);
        count = CGImageSourceGetCount(gif);
        timer = [NSTimer scheduledTimerWithTimeInterval:0.12 target:self selector:@selector(play) userInfo:nil repeats:YES];
        [timer fire];
    }
    return self;
}

-(void)play
{
    index ++;
    index = index%count;
    CGImageRef ref = CGImageSourceCreateImageAtIndex(gif, index, (CFDictionaryRef)gifProperties);
    self.layer.contents = (__bridge id)ref;
}
-(void)removeFromSuperview
{
    [timer invalidate];
    timer = nil;
    [super removeFromSuperview];
}

@end

成功跑起来后,要消耗150+内存!!

想请教一下各位,展示gif有什么优化的方法?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回覆(2)
Peter_Zhu

可以參考下 Twitter,他們將 gif 图转成了 mp4

PHPzhong

直接用 webview載入不就行了

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板