From 86cc45fd26a7f32dc74cf12a72698675444c8e23 Mon Sep 17 00:00:00 2001 From: Salil Apte Date: Fri, 26 Oct 2018 11:39:06 -0700 Subject: [PATCH] Fixes incorrectly looping gifs as well as gifs not stopping on last frame --- Libraries/Image/RCTGIFImageDecoder.m | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Libraries/Image/RCTGIFImageDecoder.m b/Libraries/Image/RCTGIFImageDecoder.m index 322d137cda733c..0943493c9a283c 100644 --- a/Libraries/Image/RCTGIFImageDecoder.m +++ b/Libraries/Image/RCTGIFImageDecoder.m @@ -32,8 +32,18 @@ - (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)imageData { CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL); NSDictionary *properties = (__bridge_transfer NSDictionary *)CGImageSourceCopyProperties(imageSource, NULL); - NSUInteger loopCount = [properties[(id)kCGImagePropertyGIFDictionary][(id)kCGImagePropertyGIFLoopCount] unsignedIntegerValue]; - + NSUInteger loopCount = 0; + if ([[properties[(id)kCGImagePropertyGIFDictionary] allKeys] containsObject:(id)kCGImagePropertyGIFLoopCount]) { + loopCount = [properties[(id)kCGImagePropertyGIFDictionary][(id)kCGImagePropertyGIFLoopCount] unsignedIntegerValue]; + if (loopCount == 0) { + // A loop count of 0 means infinite + loopCount = HUGE_VALF; + } else { + // A loop count of 1 means it should repeat twice, 2 means, thrice, etc. + loopCount += 1; + } + } + UIImage *image = nil; size_t imageCount = CGImageSourceGetCount(imageSource); if (imageCount > 1) { @@ -84,11 +94,12 @@ - (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)imageData // Create animation CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"]; animation.calculationMode = kCAAnimationDiscrete; - animation.repeatCount = loopCount == 0 ? HUGE_VALF : loopCount; + animation.repeatCount = loopCount; animation.keyTimes = keyTimes; animation.values = images; animation.duration = duration; animation.removedOnCompletion = NO; + animation.fillMode = kCAFillModeForwards; image.reactKeyframeAnimation = animation; } else {