Skip to content

Commit 7ace068

Browse files
author
Olivier Poitrey
committed
Merge branch 'master' of git://github.com/vincentjames501/SDWebImage into vincentjames501-master
2 parents 25007e0 + 8a541f6 commit 7ace068

File tree

1 file changed

+63
-28
lines changed

1 file changed

+63
-28
lines changed

SDWebImage/UIImage+GIF.m

Lines changed: 63 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ + (UIImage *)sd_animatedGIFWithData:(NSData *)data
1717
{
1818
return nil;
1919
}
20-
20+
2121
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
22-
22+
2323
size_t count = CGImageSourceGetCount(source);
2424

2525
UIImage *animatedImage;
@@ -38,8 +38,7 @@ + (UIImage *)sd_animatedGIFWithData:(NSData *)data
3838
{
3939
CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
4040

41-
NSDictionary *frameProperties = CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source, i, NULL));
42-
duration += [[[frameProperties objectForKey:(NSString*)kCGImagePropertyGIFDictionary] objectForKey:(NSString*)kCGImagePropertyGIFDelayTime] doubleValue];
41+
duration += [self frameDurationAtIndex:i source:source];
4342

4443
[images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
4544

@@ -59,45 +58,81 @@ + (UIImage *)sd_animatedGIFWithData:(NSData *)data
5958
return animatedImage;
6059
}
6160

61+
+ (float)frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source
62+
{
63+
float frameDuration = 0.1f;
64+
CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil);
65+
NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties;
66+
NSDictionary *gifProperties = frameProperties[(NSString *)kCGImagePropertyGIFDictionary];
67+
68+
NSNumber *delayTimeUnclampedProp = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
69+
if (delayTimeUnclampedProp)
70+
{
71+
frameDuration = [delayTimeUnclampedProp floatValue];
72+
}
73+
else
74+
{
75+
76+
NSNumber *delayTimeProp = gifProperties[(NSString *)kCGImagePropertyGIFDelayTime];
77+
if (delayTimeProp)
78+
{
79+
frameDuration = [delayTimeProp floatValue];
80+
}
81+
}
82+
83+
// Many annoying ads specify a 0 duration to make an image flash as quickly as possible.
84+
// We follow Firefox's behavior and use a duration of 100 ms for any frames that specify
85+
// a duration of <= 10 ms. See <rdar://problem/7689300> and <http://webkit.org/b/36082>
86+
// for more information.
87+
88+
if (frameDuration < 0.011f)
89+
{
90+
frameDuration = 0.100f;
91+
}
92+
93+
CFRelease(cfFrameProperties);
94+
return frameDuration;
95+
}
96+
6297
+ (UIImage *)sd_animatedGIFNamed:(NSString *)name
6398
{
6499
CGFloat scale = [UIScreen mainScreen].scale;
65-
100+
66101
if (scale > 1.0f)
67102
{
68103
NSString *retinaPath = [[NSBundle mainBundle] pathForResource:[name stringByAppendingString:@"@2x"] ofType:@"gif"];
69-
104+
70105
NSData *data = [NSData dataWithContentsOfFile:retinaPath];
71-
106+
72107
if (data)
73108
{
74109
return [UIImage sd_animatedGIFWithData:data];
75110
}
76-
111+
77112
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
78-
113+
79114
data = [NSData dataWithContentsOfFile:path];
80-
115+
81116
if (data)
82117
{
83118
return [UIImage sd_animatedGIFWithData:data];
84119
}
85-
120+
86121
return [UIImage imageNamed:name];
87122
}
88123
else
89124
{
90125
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
91-
126+
92127
NSData *data = [NSData dataWithContentsOfFile:path];
93-
128+
94129
if (data)
95130
{
96131
return [UIImage sd_animatedGIFWithData:data];
97132
}
98-
133+
99134
return [UIImage imageNamed:name];
100-
}
135+
}
101136
}
102137

103138
- (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size
@@ -106,40 +141,40 @@ - (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size
106141
{
107142
return self;
108143
}
109-
144+
110145
CGSize scaledSize = size;
111-
CGPoint thumbnailPoint = CGPointZero;
112-
146+
CGPoint thumbnailPoint = CGPointZero;
147+
113148
CGFloat widthFactor = size.width / self.size.width;
114149
CGFloat heightFactor = size.height / self.size.height;
115150
CGFloat scaleFactor = (widthFactor > heightFactor) ? widthFactor :heightFactor;
116151
scaledSize.width = self.size.width * scaleFactor;
117152
scaledSize.height = self.size.height * scaleFactor;
118-
153+
119154
if (widthFactor > heightFactor)
120155
{
121-
thumbnailPoint.y = (size.height - scaledSize.height) * 0.5;
156+
thumbnailPoint.y = (size.height - scaledSize.height) * 0.5;
122157
}
123158
else if (widthFactor < heightFactor)
124159
{
125160
thumbnailPoint.x = (size.width - scaledSize.width) * 0.5;
126161
}
127-
162+
128163
NSMutableArray *scaledImages = [NSMutableArray array];
129-
130-
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
131-
164+
165+
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
166+
132167
for (UIImage *image in self.images)
133168
{
134169
[image drawInRect:CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledSize.width, scaledSize.height)];
135170
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
136-
171+
137172
[scaledImages addObject:newImage];
138173
}
139-
174+
140175
UIGraphicsEndImageContext();
141-
142-
return [UIImage animatedImageWithImages:scaledImages duration:self.duration];
176+
177+
return [UIImage animatedImageWithImages:scaledImages duration:self.duration];
143178
}
144179

145180
@end

0 commit comments

Comments
 (0)