@@ -711,6 +711,18 @@ - (BOOL)scanAndCheckFramesValidWithDemuxer:(WebPDemuxer *)demuxer {
711
711
uint32_t loopCount = WebPDemuxGetI (demuxer, WEBP_FF_LOOP_COUNT);
712
712
NSMutableArray <SDWebPCoderFrame *> *frames = [NSMutableArray array ];
713
713
714
+ _hasAnimation = hasAnimation;
715
+ _hasAlpha = hasAlpha;
716
+ _canvasWidth = canvasWidth;
717
+ _canvasHeight = canvasHeight;
718
+ _frameCount = frameCount;
719
+ _loopCount = loopCount;
720
+
721
+ // If static WebP, does not need to parse the frame blend index
722
+ if (frameCount <= 1 ) {
723
+ return YES ;
724
+ }
725
+
714
726
// We should loop all the frames and scan each frames' blendFromIndex for later decoding, this can also ensure all frames is valid
715
727
do {
716
728
SDWebPCoderFrame *frame = [[SDWebPCoderFrame alloc ] init ];
@@ -748,12 +760,6 @@ - (BOOL)scanAndCheckFramesValidWithDemuxer:(WebPDemuxer *)demuxer {
748
760
return NO ;
749
761
}
750
762
_frames = [frames copy ];
751
- _hasAnimation = hasAnimation;
752
- _hasAlpha = hasAlpha;
753
- _canvasWidth = canvasWidth;
754
- _canvasHeight = canvasHeight;
755
- _frameCount = frameCount;
756
- _loopCount = loopCount;
757
763
758
764
return YES ;
759
765
}
@@ -774,6 +780,9 @@ - (NSTimeInterval)animatedImageDurationAtIndex:(NSUInteger)index {
774
780
if (index >= _frameCount) {
775
781
return 0 ;
776
782
}
783
+ if (_frameCount <= 1 ) {
784
+ return 0 ;
785
+ }
777
786
return _frames[index].duration ;
778
787
}
779
788
@@ -783,11 +792,40 @@ - (UIImage *)animatedImageFrameAtIndex:(NSUInteger)index {
783
792
return nil ;
784
793
}
785
794
SD_LOCK (_lock);
786
- image = [self safeAnimatedImageFrameAtIndex: index];
795
+ if (_frameCount <= 1 ) {
796
+ image = [self safeStaticImageFrame ];
797
+ } else {
798
+ image = [self safeAnimatedImageFrameAtIndex: index];
799
+ }
787
800
SD_UNLOCK (_lock);
788
801
return image;
789
802
}
790
803
804
+ - (UIImage *)safeStaticImageFrame {
805
+ UIImage *image;
806
+ if (!_colorSpace) {
807
+ _colorSpace = [self sd_colorSpaceWithDemuxer: _demux];
808
+ }
809
+ // Static WebP image
810
+ WebPIterator iter;
811
+ if (!WebPDemuxGetFrame (_demux, 1 , &iter)) {
812
+ WebPDemuxReleaseIterator (&iter);
813
+ return nil ;
814
+ }
815
+ CGImageRef imageRef = [self sd_createWebpImageWithData: iter.fragment colorSpace: _colorSpace];
816
+ if (!imageRef) {
817
+ return nil ;
818
+ }
819
+ #if SD_UIKIT || SD_WATCH
820
+ image = [[UIImage alloc ] initWithCGImage: imageRef scale: _scale orientation: UIImageOrientationUp];
821
+ #else
822
+ image = [[UIImage alloc ] initWithCGImage: imageRef scale: _scale orientation: kCGImagePropertyOrientationUp ];
823
+ #endif
824
+ CGImageRelease (imageRef);
825
+ WebPDemuxReleaseIterator (&iter);
826
+ return image;
827
+ }
828
+
791
829
- (UIImage *)safeAnimatedImageFrameAtIndex : (NSUInteger )index {
792
830
if (!_canvas) {
793
831
CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Host ;
0 commit comments