@@ -959,18 +959,34 @@ - (UIImage *)animatedImageFrameAtIndex:(NSUInteger)index {
959959
960960- (UIImage *)safeStaticImageFrame {
961961 UIImage *image;
962- if (!_colorSpace) {
963- _colorSpace = [self sd_createColorSpaceWithDemuxer: _demux];
964- }
965962 // Static WebP image
966963 WebPIterator iter;
967964 if (!WebPDemuxGetFrame (_demux, 1 , &iter)) {
968965 WebPDemuxReleaseIterator (&iter);
969966 return nil ;
970967 }
968+ if (!_colorSpace) {
969+ _colorSpace = [self sd_createColorSpaceWithDemuxer: _demux];
970+ }
971971 // Check whether we need to use thumbnail
972- CGSize scaledSize = SDCalculateThumbnailSize (CGSizeMake (_canvasWidth, _canvasHeight), _preserveAspectRatio, _thumbnailSize);
973- CGImageRef imageRef = [self sd_createWebpImageWithData: iter.fragment colorSpace: _colorSpace scaledSize: scaledSize];
972+ CGImageRef imageRef;
973+ if (_hasAnimation) {
974+ // If have animation, we still need to allocate a CGContext, because the poster frame may be smaller than canvas
975+ if (!_canvas) {
976+ CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Host ;
977+ bitmapInfo |= _hasAlpha ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst ;
978+ CGContextRef canvas = CGBitmapContextCreate (NULL , _canvasWidth, _canvasHeight, 8 , 0 , [SDImageCoderHelper colorSpaceGetDeviceRGB ], bitmapInfo);
979+ if (!canvas) {
980+ return nil ;
981+ }
982+ _canvas = canvas;
983+ }
984+ CGSize scaledSize = SDCalculateThumbnailSize (CGSizeMake (_canvasWidth, _canvasHeight), _preserveAspectRatio, _thumbnailSize);
985+ imageRef = [self sd_drawnWebpImageWithCanvas: _canvas iterator: iter colorSpace: _colorSpace scaledSize: scaledSize];
986+ } else {
987+ CGSize scaledSize = SDCalculateThumbnailSize (CGSizeMake (iter.width , iter.height ), _preserveAspectRatio, _thumbnailSize);
988+ imageRef = [self sd_createWebpImageWithData: iter.fragment colorSpace: _colorSpace scaledSize: scaledSize];
989+ }
974990 if (!imageRef) {
975991 return nil ;
976992 }
0 commit comments