Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions SDWebImageWebPCoder/Classes/SDImageWebPCoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -810,12 +810,10 @@ - (UIImage *)safeAnimatedImageFrameAtIndex:(NSUInteger)index {
// 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;
}
Expand All @@ -824,7 +822,6 @@ - (UIImage *)safeAnimatedImageFrameAtIndex:(NSUInteger)index {
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.
Expand All @@ -843,7 +840,13 @@ - (UIImage *)safeAnimatedImageFrameAtIndex:(NSUInteger)index {
}
} 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];
Expand Down