diff --git a/SDWebImage/UIImage+GIF.m b/SDWebImage/UIImage+GIF.m index d19fa41..99a0c88 100755 --- a/SDWebImage/UIImage+GIF.m +++ b/SDWebImage/UIImage+GIF.m @@ -17,9 +17,9 @@ { return nil; } - + CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); - + size_t count = CGImageSourceGetCount(source); UIImage *animatedImage; @@ -38,8 +38,7 @@ { CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL); - NSDictionary *frameProperties = CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source, i, NULL)); - duration += [[[frameProperties objectForKey:(NSString*)kCGImagePropertyGIFDictionary] objectForKey:(NSString*)kCGImagePropertyGIFDelayTime] doubleValue]; + duration += [self frameDurationAtIndex:i source:source]; [images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]]; @@ -59,45 +58,81 @@ return animatedImage; } ++ (float)frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source +{ + float frameDuration = 0.1f; + CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil); + NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties; + NSDictionary *gifProperties = frameProperties[(NSString *)kCGImagePropertyGIFDictionary]; + + NSNumber *delayTimeUnclampedProp = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime]; + if (delayTimeUnclampedProp) + { + frameDuration = [delayTimeUnclampedProp floatValue]; + } + else + { + + NSNumber *delayTimeProp = gifProperties[(NSString *)kCGImagePropertyGIFDelayTime]; + if (delayTimeProp) + { + frameDuration = [delayTimeProp floatValue]; + } + } + + // Many annoying ads specify a 0 duration to make an image flash as quickly as possible. + // We follow Firefox's behavior and use a duration of 100 ms for any frames that specify + // a duration of <= 10 ms. See and + // for more information. + + if (frameDuration < 0.011f) + { + frameDuration = 0.100f; + } + + CFRelease(cfFrameProperties); + return frameDuration; +} + + (UIImage *)sd_animatedGIFNamed:(NSString *)name { CGFloat scale = [UIScreen mainScreen].scale; - + if (scale > 1.0f) { NSString *retinaPath = [[NSBundle mainBundle] pathForResource:[name stringByAppendingString:@"@2x"] ofType:@"gif"]; - + NSData *data = [NSData dataWithContentsOfFile:retinaPath]; - + if (data) { return [UIImage sd_animatedGIFWithData:data]; } - + NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"]; - + data = [NSData dataWithContentsOfFile:path]; - + if (data) { return [UIImage sd_animatedGIFWithData:data]; } - + return [UIImage imageNamed:name]; } else { NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"]; - + NSData *data = [NSData dataWithContentsOfFile:path]; - + if (data) { return [UIImage sd_animatedGIFWithData:data]; } - + return [UIImage imageNamed:name]; - } + } } - (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size @@ -106,40 +141,40 @@ { return self; } - + CGSize scaledSize = size; - CGPoint thumbnailPoint = CGPointZero; - + CGPoint thumbnailPoint = CGPointZero; + CGFloat widthFactor = size.width / self.size.width; CGFloat heightFactor = size.height / self.size.height; CGFloat scaleFactor = (widthFactor > heightFactor) ? widthFactor :heightFactor; scaledSize.width = self.size.width * scaleFactor; scaledSize.height = self.size.height * scaleFactor; - + if (widthFactor > heightFactor) { - thumbnailPoint.y = (size.height - scaledSize.height) * 0.5; + thumbnailPoint.y = (size.height - scaledSize.height) * 0.5; } else if (widthFactor < heightFactor) { thumbnailPoint.x = (size.width - scaledSize.width) * 0.5; } - + NSMutableArray *scaledImages = [NSMutableArray array]; - - UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); - + + UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); + for (UIImage *image in self.images) { [image drawInRect:CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledSize.width, scaledSize.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); - + [scaledImages addObject:newImage]; } - + UIGraphicsEndImageContext(); - - return [UIImage animatedImageWithImages:scaledImages duration:self.duration]; + + return [UIImage animatedImageWithImages:scaledImages duration:self.duration]; } @end