搜索

首页  >  问答  >  正文

ios - `Gif`动画内存暴增!

最近要展示一个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有什么优化的方法?

大家讲道理大家讲道理2893 天前294

全部回复(2)我来回复

  • 天蓬老师

    天蓬老师2017-04-17 17:47:24

    可以参考下 Twitter,他们将 gif 图转成了 mp4

    回复
    0
  • PHPz

    PHPz2017-04-17 17:47:24

    直接用 webview加载不就行了

    回复
    0
  • 取消回复