@@ -959,18 +959,34 @@ - (UIImage *)animatedImageFrameAtIndex:(NSUInteger)index {
959
959
960
960
- (UIImage *)safeStaticImageFrame {
961
961
UIImage *image;
962
- if (!_colorSpace) {
963
- _colorSpace = [self sd_createColorSpaceWithDemuxer: _demux];
964
- }
965
962
// Static WebP image
966
963
WebPIterator iter;
967
964
if (!WebPDemuxGetFrame (_demux, 1 , &iter)) {
968
965
WebPDemuxReleaseIterator (&iter);
969
966
return nil ;
970
967
}
968
+ if (!_colorSpace) {
969
+ _colorSpace = [self sd_createColorSpaceWithDemuxer: _demux];
970
+ }
971
971
// 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
+ }
974
990
if (!imageRef) {
975
991
return nil ;
976
992
}
0 commit comments