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

最近要展示一个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

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

PHPzhong

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

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿