pinterest / PINRemoteImage

A thread safe, performant, feature rich image fetcher
Apache License 2.0
4.01k stars 511 forks source link

Pause the playback of GIF when scroll and resume when scroll stopped #520

Open zhongwuzw opened 5 years ago

zhongwuzw commented 5 years ago

I have the requirement to improve the scroll performance when collection view has many GIFs, thus pause the playback in scrolling state and resume after scroll finished. (By setting runloop mode to default mode other than common mode)

For example, a gif has 20 frames, frame interval 16ms, collection view not scrolled, now it's playing 5th frame, and scroll started, after scroll finished, maybe 5s passed, it started to resume, and even 5s passed, it still need to play 6th frame.

But in our implementation currently, we use absolute time(CACurrentMediaTime) to calculate which frame we should play, leads to frame skip. Another simple example, if a gif's loop count is 1(play only once), 20 frames, frame interval 16ms, after it played 1th frame, scroll started, keep runloop mode to default 3s, after that, gif would not resume any more, because elapsed time already exceed total duration of gif.

@garrettmoon Any reason why we choose absolute time to calculate frame index? seems all other third frameworks like YYImage/FLAnimatedImage... use CADisplayLink.duration.