찾다

 >  Q&A  >  본문

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일 전296

모든 응답(2)나는 대답할 것이다

  • 天蓬老师

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

    트위터를 참고하시면 gif 사진을 mp4로 바꿨습니다.

    회신하다
    0
  • PHPz

    PHPz2017-04-17 17:47:24

    그냥 웹뷰로 바로 불러올 수는 없나요

    회신하다
    0
  • 취소회신하다