imageView添加gif图片 & 对FLAnimatedImage 源码分析

添加 gif动态图片

1. 使用 iamgeView的Animation 数组  

2.

letpath =NSBundle.mainBundle().pathForResource("like", ofType:"gif")

let data =NSData(contentsOfFile: path!)

likeImageView.image = UIImage.sd_animatedGIFWithData(data)

(SDwebImage 加载gif 会使得 内存暴增,无法消失, 实际是对 1 封装)


webView 加载 gif 无法改变 gif 的位置

3. 采用CADisplayLink 帧间隙   定时刷新imageview.layer

(性能比较好 不会造成内存暴增)

pod 'FLAnimatedImage' 采用的是4的方案,性能比较好


@property (weak, nonatomic) IBOutlet FLAnimatedImageView *imageView;


NSString *path = [[NSBundle mainBundle] pathForResource:@"loading" ofType:@"gif"];
    FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:[NSData dataWithContentsOfFile: path]];

    self.imageView.animatedImage = image;

下面时 在使用中 对 FLAnimatedImage的源码分析

 1. 先对 gif 每帧绘制新的image 并存储,并存储对应的index 以及相应的时间,
2. 创建 CADisplayLink ,设置相应的帧间隔

  // 计算 每帧相关时间的最大公约数,相当于最少时间的帧,所需要的帧的间隔需要整除,便可以及时刷新到相关的帧,如果较大,则没有必要每帧都刷新,因为下次可能还是这一帧

 self.displayLink.frameInterval = MAX([self frameDelayGreatestCommonDivisor] * kDisplayRefreshRate, 1);
3.在帧刷新的 方法中
   计算两次调用的时间间隔 (duration:每帧的间隔,frameInterval:间隔帧的间隔-用来设置间隔多少帧调用一次selector方法))
   displayLink.duration * displayLink.frameInterval;
如果这个时间 > 当前帧需要的时间,则清除相关数据

下面 注释几个方法的含义:

//wyj 获取gif的基本数据
        NSDictionary *imageProperties = (__bridge_transfer NSDictionary *)CGImageSourceCopyProperties(_imageSource, NULL);
        
        //wyj 获取gifImage的基本数据
        // [imageProperties objectForKey:(id)kCGImagePropertyGIFDictionary]
        
        //wyj 获取gif的播放次数 0-循环次数
        _loopCount = [[[imageProperties objectForKey:(id)kCGImagePropertyGIFDictionary] objectForKey:(id)kCGImagePropertyGIFLoopCount] unsignedIntegerValue];
        
        //wyj 获取gif的帧数
        size_t imageCount = CGImageSourceGetCount(_imageSource);
        
        //wyj skippedFrameCount 跳过的帧数 (针对于其中对应 无效数据的记录)
        NSUInteger skippedFrameCount = 0;


    






版权声明:本文为xiyu_xiaoxiao原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。