Skip to content

Commit b4e72dd

Browse files
authored
Merge pull request #23 from SDWebImage/fix_animated_webp_frame_blend_issue
Recheck the animated canvas calculation logic, fix the issue of that calculation
2 parents c5b98c4 + 3e3b7e3 commit b4e72dd

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

SDWebImageWebPCoder/Classes/SDImageWebPCoder.m

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -810,12 +810,10 @@ - (UIImage *)safeAnimatedImageFrameAtIndex:(NSUInteger)index {
810810
// 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.
811811
// So, we calculate each frame's `blendFromIndex`. Then directly draw canvas from that index, instead of always from 0 index.
812812

813-
if (_currentBlendIndex + 1 == index) {
813+
if (_currentBlendIndex != NSNotFound && _currentBlendIndex + 1 == index) {
814814
// If the request index is subsequence of current blend index, it does not matter what dispose method is. The canvas is always ready.
815-
_currentBlendIndex = index;
816-
NSUInteger startIndex = index;
817815
// libwebp's index start with 1
818-
if (!WebPDemuxGetFrame(_demux, (int)(startIndex + 1), &iter)) {
816+
if (!WebPDemuxGetFrame(_demux, (int)(index + 1), &iter)) {
819817
WebPDemuxReleaseIterator(&iter);
820818
return nil;
821819
}
@@ -824,7 +822,6 @@ - (UIImage *)safeAnimatedImageFrameAtIndex:(NSUInteger)index {
824822
if (_currentBlendIndex != NSNotFound) {
825823
CGContextClearRect(_canvas, CGRectMake(0, 0, _canvasWidth, _canvasHeight));
826824
}
827-
_currentBlendIndex = index;
828825

829826
// Then, loop from the blend from index, draw each of previous frames on the canvas.
830827
// We use do while loop to call `WebPDemuxNextFrame`(fast), until the endIndex meet.
@@ -843,7 +840,13 @@ - (UIImage *)safeAnimatedImageFrameAtIndex:(NSUInteger)index {
843840
}
844841
} while ((size_t)iter.frame_num < endIndex && WebPDemuxNextFrame(&iter));
845842
}
843+
// libwebp's index start with 1
844+
if (!WebPDemuxGetFrame(_demux, (int)(index + 1), &iter)) {
845+
WebPDemuxReleaseIterator(&iter);
846+
return nil;
847+
}
846848
}
849+
_currentBlendIndex = index;
847850

848851
// Now the canvas is ready, which respects of dispose method behavior. Just do normal decoding and produce image.
849852
CGImageRef imageRef = [self sd_drawnWebpImageWithCanvas:_canvas iterator:iter colorSpace:_colorSpace];

0 commit comments

Comments
 (0)