From 3e3b7e30b100b40276553213e379ec939c9d2545 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Thu, 12 Sep 2019 14:09:26 +0800 Subject: [PATCH] Recheck the animated canvas calculation logic, fix the issue of that calculation --- SDWebImageWebPCoder/Classes/SDImageWebPCoder.m | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m b/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m index f37664a..617bdb2 100644 --- a/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m +++ b/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m @@ -810,12 +810,10 @@ static void FreeImageData(void *info, const void *data, size_t size) { // But when one frame's dispose method is `WEBP_MUX_DISPOSE_BACKGROUND`, the canvas is cleared after the frame decoded. And subsequent frames are not effected by that frame. // So, we calculate each frame's `blendFromIndex`. Then directly draw canvas from that index, instead of always from 0 index. - if (_currentBlendIndex + 1 == index) { + if (_currentBlendIndex != NSNotFound && _currentBlendIndex + 1 == index) { // If the request index is subsequence of current blend index, it does not matter what dispose method is. The canvas is always ready. - _currentBlendIndex = index; - NSUInteger startIndex = index; // libwebp's index start with 1 - if (!WebPDemuxGetFrame(_demux, (int)(startIndex + 1), &iter)) { + if (!WebPDemuxGetFrame(_demux, (int)(index + 1), &iter)) { WebPDemuxReleaseIterator(&iter); return nil; } @@ -824,7 +822,6 @@ static void FreeImageData(void *info, const void *data, size_t size) { if (_currentBlendIndex != NSNotFound) { CGContextClearRect(_canvas, CGRectMake(0, 0, _canvasWidth, _canvasHeight)); } - _currentBlendIndex = index; // Then, loop from the blend from index, draw each of previous frames on the canvas. // We use do while loop to call `WebPDemuxNextFrame`(fast), until the endIndex meet. @@ -843,7 +840,13 @@ static void FreeImageData(void *info, const void *data, size_t size) { } } while ((size_t)iter.frame_num < endIndex && WebPDemuxNextFrame(&iter)); } + // libwebp's index start with 1 + if (!WebPDemuxGetFrame(_demux, (int)(index + 1), &iter)) { + WebPDemuxReleaseIterator(&iter); + return nil; + } } + _currentBlendIndex = index; // Now the canvas is ready, which respects of dispose method behavior. Just do normal decoding and produce image. CGImageRef imageRef = [self sd_drawnWebpImageWithCanvas:_canvas iterator:iter colorSpace:_colorSpace];