Skip to content

Commit 1dbb79f

Browse files
author
Olivier Poitrey
committed
Refactor GIF support
Remove GIF specific code from main source, isolate it in a UIImage category to ease future addition of format support
1 parent 447f1cc commit 1dbb79f

File tree

8 files changed

+113
-53
lines changed

8 files changed

+113
-53
lines changed

SDWebImage.xcodeproj/project.pbxproj

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@
6767
5376131E155AD0D5005750A4 /* SDWebImagePrefetcher.h in Headers */ = {isa = PBXBuildFile; fileRef = 53922D91148C56230056699D /* SDWebImagePrefetcher.h */; settings = {ATTRIBUTES = (Public, ); }; };
6868
5376131F155AD0D5005750A4 /* UIButton+WebCache.h in Headers */ = {isa = PBXBuildFile; fileRef = 53922D93148C56230056699D /* UIButton+WebCache.h */; settings = {ATTRIBUTES = (Public, ); }; };
6969
53761320155AD0D5005750A4 /* UIImageView+WebCache.h in Headers */ = {isa = PBXBuildFile; fileRef = 53922D95148C56230056699D /* UIImageView+WebCache.h */; settings = {ATTRIBUTES = (Public, ); }; };
70+
53EDFB8A17623F7C00698166 /* UIImage+MultiFormat.h in Headers */ = {isa = PBXBuildFile; fileRef = 53EDFB8817623F7C00698166 /* UIImage+MultiFormat.h */; };
71+
53EDFB8B17623F7C00698166 /* UIImage+MultiFormat.h in Headers */ = {isa = PBXBuildFile; fileRef = 53EDFB8817623F7C00698166 /* UIImage+MultiFormat.h */; };
72+
53EDFB8C17623F7C00698166 /* UIImage+MultiFormat.m in Sources */ = {isa = PBXBuildFile; fileRef = 53EDFB8917623F7C00698166 /* UIImage+MultiFormat.m */; };
73+
53EDFB8D17623F7C00698166 /* UIImage+MultiFormat.m in Sources */ = {isa = PBXBuildFile; fileRef = 53EDFB8917623F7C00698166 /* UIImage+MultiFormat.m */; };
7074
A18A6CC7172DC28500419892 /* UIImage+GIF.h in Headers */ = {isa = PBXBuildFile; fileRef = A18A6CC5172DC28500419892 /* UIImage+GIF.h */; };
7175
A18A6CC8172DC28500419892 /* UIImage+GIF.h in Headers */ = {isa = PBXBuildFile; fileRef = A18A6CC5172DC28500419892 /* UIImage+GIF.h */; };
7276
A18A6CC9172DC28500419892 /* UIImage+GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = A18A6CC6172DC28500419892 /* UIImage+GIF.m */; };
@@ -112,6 +116,8 @@
112116
53922D94148C56230056699D /* UIButton+WebCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIButton+WebCache.m"; path = "SDWebImage/UIButton+WebCache.m"; sourceTree = SOURCE_ROOT; };
113117
53922D95148C56230056699D /* UIImageView+WebCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIImageView+WebCache.h"; path = "SDWebImage/UIImageView+WebCache.h"; sourceTree = SOURCE_ROOT; };
114118
53922D96148C56230056699D /* UIImageView+WebCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIImageView+WebCache.m"; path = "SDWebImage/UIImageView+WebCache.m"; sourceTree = SOURCE_ROOT; };
119+
53EDFB8817623F7C00698166 /* UIImage+MultiFormat.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+MultiFormat.h"; sourceTree = "<group>"; };
120+
53EDFB8917623F7C00698166 /* UIImage+MultiFormat.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+MultiFormat.m"; sourceTree = "<group>"; };
115121
53FB893F14D35D1A0020B787 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
116122
53FB894814D35E9E0020B787 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
117123
A18A6CC5172DC28500419892 /* UIImage+GIF.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+GIF.h"; sourceTree = "<group>"; };
@@ -190,14 +196,16 @@
190196
53922DA9148C562D0056699D /* Categories */ = {
191197
isa = PBXGroup;
192198
children = (
193-
535699B415113E7300A4C397 /* MKAnnotationView+WebCache.h */,
194-
535699B515113E7300A4C397 /* MKAnnotationView+WebCache.m */,
199+
53EDFB8817623F7C00698166 /* UIImage+MultiFormat.h */,
200+
53EDFB8917623F7C00698166 /* UIImage+MultiFormat.m */,
195201
A18A6CCB172DC33A00419892 /* NSData+GIF.h */,
196202
A18A6CCC172DC33A00419892 /* NSData+GIF.m */,
197-
53922D93148C56230056699D /* UIButton+WebCache.h */,
198-
53922D94148C56230056699D /* UIButton+WebCache.m */,
199203
A18A6CC5172DC28500419892 /* UIImage+GIF.h */,
200204
A18A6CC6172DC28500419892 /* UIImage+GIF.m */,
205+
535699B415113E7300A4C397 /* MKAnnotationView+WebCache.h */,
206+
535699B515113E7300A4C397 /* MKAnnotationView+WebCache.m */,
207+
53922D93148C56230056699D /* UIButton+WebCache.h */,
208+
53922D94148C56230056699D /* UIButton+WebCache.m */,
201209
53922D95148C56230056699D /* UIImageView+WebCache.h */,
202210
53922D96148C56230056699D /* UIImageView+WebCache.m */,
203211
);
@@ -257,6 +265,7 @@
257265
530E49EB16464C7F002868E7 /* SDWebImageDownloaderOperation.h in Headers */,
258266
A18A6CC8172DC28500419892 /* UIImage+GIF.h in Headers */,
259267
A18A6CCE172DC33A00419892 /* NSData+GIF.h in Headers */,
268+
53EDFB8B17623F7C00698166 /* UIImage+MultiFormat.h in Headers */,
260269
);
261270
runOnlyForDeploymentPostprocessing = 0;
262271
};
@@ -276,6 +285,7 @@
276285
530E49EA16464C7C002868E7 /* SDWebImageDownloaderOperation.h in Headers */,
277286
A18A6CC7172DC28500419892 /* UIImage+GIF.h in Headers */,
278287
A18A6CCD172DC33A00419892 /* NSData+GIF.h in Headers */,
288+
53EDFB8A17623F7C00698166 /* UIImage+MultiFormat.h in Headers */,
279289
);
280290
runOnlyForDeploymentPostprocessing = 0;
281291
};
@@ -392,6 +402,7 @@
392402
530E49ED16464C84002868E7 /* SDWebImageDownloaderOperation.m in Sources */,
393403
A18A6CCA172DC28500419892 /* UIImage+GIF.m in Sources */,
394404
A18A6CD0172DC33A00419892 /* NSData+GIF.m in Sources */,
405+
53EDFB8D17623F7C00698166 /* UIImage+MultiFormat.m in Sources */,
395406
);
396407
runOnlyForDeploymentPostprocessing = 0;
397408
};
@@ -410,6 +421,7 @@
410421
53406750167780C40042B59E /* SDWebImageCompat.m in Sources */,
411422
A18A6CC9172DC28500419892 /* UIImage+GIF.m in Sources */,
412423
A18A6CCF172DC33A00419892 /* NSData+GIF.m in Sources */,
424+
53EDFB8C17623F7C00698166 /* UIImage+MultiFormat.m in Sources */,
413425
);
414426
runOnlyForDeploymentPostprocessing = 0;
415427
};

SDWebImage/SDImageCache.m

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#import "SDImageCache.h"
1010
#import "SDWebImageDecoder.h"
11-
#import "UIImage+GIF.h"
11+
#import "UIImage+MultiFormat.h"
1212
#import <CommonCrypto/CommonDigest.h>
1313
#import <mach/mach.h>
1414
#import <mach/mach_host.h>
@@ -226,17 +226,10 @@ - (UIImage *)diskImageForKey:(NSString *)key
226226
NSData *data = [self diskImageDataBySearchingAllPathsForKey:key];
227227
if (data)
228228
{
229-
if ([data sd_isGIF])
230-
{
231-
UIImage *image = [UIImage sd_animatedGIFWithData:data];
232-
return [self scaledImageForKey:key image:image];
233-
}
234-
else
235-
{
236-
UIImage *image = [[UIImage alloc] initWithData:data];
237-
UIImage *scaledImage = [self scaledImageForKey:key image:image];
238-
return [UIImage decodedImageWithImage:scaledImage];
239-
}
229+
UIImage *image = [UIImage sd_imageWithData:data];
230+
image = [self scaledImageForKey:key image:image];
231+
image = [UIImage decodedImageWithImage:image];
232+
return image;
240233
}
241234
else
242235
{

SDWebImage/SDWebImageDecoder.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313
@implementation UIImage (ForceDecode)
1414

1515
+ (UIImage *)decodedImageWithImage:(UIImage *)image
16-
{
16+
{
17+
if (image.images)
18+
{
19+
// Do not decode animated images
20+
return image;
21+
}
22+
1723
CGImageRef imageRef = image.CGImage;
1824
CGSize imageSize = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
1925
CGRect imageRect = (CGRect){.origin = CGPointZero, .size = imageSize};

SDWebImage/SDWebImageDownloaderOperation.m

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#import "SDWebImageDownloaderOperation.h"
1010
#import "SDWebImageDecoder.h"
11-
#import "UIImage+GIF.h"
11+
#import "UIImage+MultiFormat.h"
1212
#import <ImageIO/ImageIO.h>
1313

1414
@interface SDWebImageDownloaderOperation ()
@@ -273,21 +273,13 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
273273
}
274274
else
275275
{
276-
BOOL isImageGIF = [self.imageData sd_isGIF];
277276

278-
UIImage *image;
279-
if (isImageGIF)
280-
{
281-
image = [UIImage sd_animatedGIFWithData:self.imageData];
282-
}
283-
else
284-
{
285-
image = [[UIImage alloc] initWithData:self.imageData];
286-
}
277+
UIImage *image = [UIImage sd_imageWithData:self.imageData];
287278

288279
image = [self scaledImageForKey:self.request.URL.absoluteString image:image];
289280

290-
if (!isImageGIF) {
281+
if (!image.images) // Do not force decod animated GIFs
282+
{
291283
image = [UIImage decodedImageWithImage:image];
292284
}
293285

SDWebImage/SDWebImageManager.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,12 @@ - (NSString *)cacheKeyForURL:(NSURL *)url
156156
{
157157
// Image refresh hit the NSURLCache cache, do not call the completion block
158158
}
159-
else if (downloadedImage && [self.delegate respondsToSelector:@selector(imageManager:transformDownloadedImage:withURL:)])
159+
// NOTE: We don't call transformDownloadedImage delegate method on animated images as most transformation code would mangle it
160+
else if (downloadedImage && !downloadedImage.images && [self.delegate respondsToSelector:@selector(imageManager:transformDownloadedImage:withURL:)])
160161
{
161162
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^
162-
{
163-
BOOL isImageGIF = [data sd_isGIF];
164-
UIImage *transformedImage = isImageGIF ? downloadedImage : [self.delegate imageManager:self transformDownloadedImage:downloadedImage withURL:url];
163+
{
164+
UIImage *transformedImage = [self.delegate imageManager:self transformDownloadedImage:downloadedImage withURL:url];
165165

166166
dispatch_async(dispatch_get_main_queue(), ^
167167
{

SDWebImage/UIImage+GIF.m

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,42 @@ + (UIImage *)sd_animatedGIFWithData:(NSData *)data
2121
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
2222

2323
size_t count = CGImageSourceGetCount(source);
24-
NSMutableArray *images = [NSMutableArray array];
25-
26-
NSTimeInterval duration = 0.0f;
27-
28-
for (size_t i = 0; i < count; i++)
24+
25+
UIImage *animatedImage;
26+
27+
if (count <= 1)
2928
{
30-
CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
31-
32-
NSDictionary *frameProperties = CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source, i, NULL));
33-
duration += [[[frameProperties objectForKey:(NSString*)kCGImagePropertyGIFDictionary] objectForKey:(NSString*)kCGImagePropertyGIFDelayTime] doubleValue];
34-
35-
[images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
36-
37-
CGImageRelease(image);
29+
animatedImage = [[UIImage alloc] initWithData:data];
3830
}
39-
40-
CFRelease(source);
41-
42-
if (!duration)
31+
else
4332
{
44-
duration = (1.0f/10.0f)*count;
33+
NSMutableArray *images = [NSMutableArray array];
34+
35+
NSTimeInterval duration = 0.0f;
36+
37+
for (size_t i = 0; i < count; i++)
38+
{
39+
CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
40+
41+
NSDictionary *frameProperties = CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source, i, NULL));
42+
duration += [[[frameProperties objectForKey:(NSString*)kCGImagePropertyGIFDictionary] objectForKey:(NSString*)kCGImagePropertyGIFDelayTime] doubleValue];
43+
44+
[images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
45+
46+
CGImageRelease(image);
47+
}
48+
49+
if (!duration)
50+
{
51+
duration = (1.0f/10.0f)*count;
52+
}
53+
54+
animatedImage = [UIImage animatedImageWithImages:images duration:duration];
4555
}
46-
47-
return [UIImage animatedImageWithImages:images duration:duration];
56+
57+
CFRelease(source);
58+
59+
return animatedImage;
4860
}
4961

5062
+ (UIImage *)sd_animatedGIFNamed:(NSString *)name

SDWebImage/UIImage+MultiFormat.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// UIImage+MultiFormat.h
3+
// SDWebImage
4+
//
5+
// Created by Olivier Poitrey on 07/06/13.
6+
// Copyright (c) 2013 Dailymotion. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface UIImage (MultiFormat)
12+
13+
+ (UIImage *)sd_imageWithData:(NSData *)data;
14+
15+
@end

SDWebImage/UIImage+MultiFormat.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// UIImage+MultiFormat.m
3+
// SDWebImage
4+
//
5+
// Created by Olivier Poitrey on 07/06/13.
6+
// Copyright (c) 2013 Dailymotion. All rights reserved.
7+
//
8+
9+
#import "UIImage+MultiFormat.h"
10+
#import "UIImage+GIF.h"
11+
12+
@implementation UIImage (MultiFormat)
13+
14+
+ (UIImage *)sd_imageWithData:(NSData *)data
15+
{
16+
UIImage *image;
17+
18+
if ([data sd_isGIF])
19+
{
20+
image = [UIImage sd_animatedGIFWithData:data];
21+
}
22+
else
23+
{
24+
image = [[UIImage alloc] initWithData:data];
25+
}
26+
27+
return image;
28+
}
29+
30+
@end

0 commit comments

Comments
 (0)