Permalink
Browse files

Refactor GIF support

Remove GIF specific code from main source, isolate it in a UIImage category to ease future addition of format support
  • Loading branch information...
1 parent 447f1cc commit 1dbb79f903b44c4330a4ee17123540ad6f322ae8 @rs committed Jun 7, 2013
@@ -67,6 +67,10 @@
5376131E155AD0D5005750A4 /* SDWebImagePrefetcher.h in Headers */ = {isa = PBXBuildFile; fileRef = 53922D91148C56230056699D /* SDWebImagePrefetcher.h */; settings = {ATTRIBUTES = (Public, ); }; };
5376131F155AD0D5005750A4 /* UIButton+WebCache.h in Headers */ = {isa = PBXBuildFile; fileRef = 53922D93148C56230056699D /* UIButton+WebCache.h */; settings = {ATTRIBUTES = (Public, ); }; };
53761320155AD0D5005750A4 /* UIImageView+WebCache.h in Headers */ = {isa = PBXBuildFile; fileRef = 53922D95148C56230056699D /* UIImageView+WebCache.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ 53EDFB8A17623F7C00698166 /* UIImage+MultiFormat.h in Headers */ = {isa = PBXBuildFile; fileRef = 53EDFB8817623F7C00698166 /* UIImage+MultiFormat.h */; };
+ 53EDFB8B17623F7C00698166 /* UIImage+MultiFormat.h in Headers */ = {isa = PBXBuildFile; fileRef = 53EDFB8817623F7C00698166 /* UIImage+MultiFormat.h */; };
+ 53EDFB8C17623F7C00698166 /* UIImage+MultiFormat.m in Sources */ = {isa = PBXBuildFile; fileRef = 53EDFB8917623F7C00698166 /* UIImage+MultiFormat.m */; };
+ 53EDFB8D17623F7C00698166 /* UIImage+MultiFormat.m in Sources */ = {isa = PBXBuildFile; fileRef = 53EDFB8917623F7C00698166 /* UIImage+MultiFormat.m */; };
A18A6CC7172DC28500419892 /* UIImage+GIF.h in Headers */ = {isa = PBXBuildFile; fileRef = A18A6CC5172DC28500419892 /* UIImage+GIF.h */; };
A18A6CC8172DC28500419892 /* UIImage+GIF.h in Headers */ = {isa = PBXBuildFile; fileRef = A18A6CC5172DC28500419892 /* UIImage+GIF.h */; };
A18A6CC9172DC28500419892 /* UIImage+GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = A18A6CC6172DC28500419892 /* UIImage+GIF.m */; };
@@ -112,6 +116,8 @@
53922D94148C56230056699D /* UIButton+WebCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIButton+WebCache.m"; path = "SDWebImage/UIButton+WebCache.m"; sourceTree = SOURCE_ROOT; };
53922D95148C56230056699D /* UIImageView+WebCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIImageView+WebCache.h"; path = "SDWebImage/UIImageView+WebCache.h"; sourceTree = SOURCE_ROOT; };
53922D96148C56230056699D /* UIImageView+WebCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIImageView+WebCache.m"; path = "SDWebImage/UIImageView+WebCache.m"; sourceTree = SOURCE_ROOT; };
+ 53EDFB8817623F7C00698166 /* UIImage+MultiFormat.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+MultiFormat.h"; sourceTree = "<group>"; };
+ 53EDFB8917623F7C00698166 /* UIImage+MultiFormat.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+MultiFormat.m"; sourceTree = "<group>"; };
53FB893F14D35D1A0020B787 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
53FB894814D35E9E0020B787 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
A18A6CC5172DC28500419892 /* UIImage+GIF.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+GIF.h"; sourceTree = "<group>"; };
@@ -190,14 +196,16 @@
53922DA9148C562D0056699D /* Categories */ = {
isa = PBXGroup;
children = (
- 535699B415113E7300A4C397 /* MKAnnotationView+WebCache.h */,
- 535699B515113E7300A4C397 /* MKAnnotationView+WebCache.m */,
+ 53EDFB8817623F7C00698166 /* UIImage+MultiFormat.h */,
+ 53EDFB8917623F7C00698166 /* UIImage+MultiFormat.m */,
A18A6CCB172DC33A00419892 /* NSData+GIF.h */,
A18A6CCC172DC33A00419892 /* NSData+GIF.m */,
- 53922D93148C56230056699D /* UIButton+WebCache.h */,
- 53922D94148C56230056699D /* UIButton+WebCache.m */,
A18A6CC5172DC28500419892 /* UIImage+GIF.h */,
A18A6CC6172DC28500419892 /* UIImage+GIF.m */,
+ 535699B415113E7300A4C397 /* MKAnnotationView+WebCache.h */,
+ 535699B515113E7300A4C397 /* MKAnnotationView+WebCache.m */,
+ 53922D93148C56230056699D /* UIButton+WebCache.h */,
+ 53922D94148C56230056699D /* UIButton+WebCache.m */,
53922D95148C56230056699D /* UIImageView+WebCache.h */,
53922D96148C56230056699D /* UIImageView+WebCache.m */,
);
@@ -257,6 +265,7 @@
530E49EB16464C7F002868E7 /* SDWebImageDownloaderOperation.h in Headers */,
A18A6CC8172DC28500419892 /* UIImage+GIF.h in Headers */,
A18A6CCE172DC33A00419892 /* NSData+GIF.h in Headers */,
+ 53EDFB8B17623F7C00698166 /* UIImage+MultiFormat.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -276,6 +285,7 @@
530E49EA16464C7C002868E7 /* SDWebImageDownloaderOperation.h in Headers */,
A18A6CC7172DC28500419892 /* UIImage+GIF.h in Headers */,
A18A6CCD172DC33A00419892 /* NSData+GIF.h in Headers */,
+ 53EDFB8A17623F7C00698166 /* UIImage+MultiFormat.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -392,6 +402,7 @@
530E49ED16464C84002868E7 /* SDWebImageDownloaderOperation.m in Sources */,
A18A6CCA172DC28500419892 /* UIImage+GIF.m in Sources */,
A18A6CD0172DC33A00419892 /* NSData+GIF.m in Sources */,
+ 53EDFB8D17623F7C00698166 /* UIImage+MultiFormat.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -410,6 +421,7 @@
53406750167780C40042B59E /* SDWebImageCompat.m in Sources */,
A18A6CC9172DC28500419892 /* UIImage+GIF.m in Sources */,
A18A6CCF172DC33A00419892 /* NSData+GIF.m in Sources */,
+ 53EDFB8C17623F7C00698166 /* UIImage+MultiFormat.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
@@ -8,7 +8,7 @@
#import "SDImageCache.h"
#import "SDWebImageDecoder.h"
-#import "UIImage+GIF.h"
+#import "UIImage+MultiFormat.h"
#import <CommonCrypto/CommonDigest.h>
#import <mach/mach.h>
#import <mach/mach_host.h>
@@ -226,17 +226,10 @@ - (UIImage *)diskImageForKey:(NSString *)key
NSData *data = [self diskImageDataBySearchingAllPathsForKey:key];
if (data)
{
- if ([data sd_isGIF])
- {
- UIImage *image = [UIImage sd_animatedGIFWithData:data];
- return [self scaledImageForKey:key image:image];
- }
- else
- {
- UIImage *image = [[UIImage alloc] initWithData:data];
- UIImage *scaledImage = [self scaledImageForKey:key image:image];
- return [UIImage decodedImageWithImage:scaledImage];
- }
+ UIImage *image = [UIImage sd_imageWithData:data];
+ image = [self scaledImageForKey:key image:image];
+ image = [UIImage decodedImageWithImage:image];
+ return image;
}
else
{
@@ -13,7 +13,13 @@
@implementation UIImage (ForceDecode)
+ (UIImage *)decodedImageWithImage:(UIImage *)image
-{
+{
+ if (image.images)
+ {
+ // Do not decode animated images
+ return image;
+ }
+
CGImageRef imageRef = image.CGImage;
CGSize imageSize = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
CGRect imageRect = (CGRect){.origin = CGPointZero, .size = imageSize};
@@ -8,7 +8,7 @@
#import "SDWebImageDownloaderOperation.h"
#import "SDWebImageDecoder.h"
-#import "UIImage+GIF.h"
+#import "UIImage+MultiFormat.h"
#import <ImageIO/ImageIO.h>
@interface SDWebImageDownloaderOperation ()
@@ -273,21 +273,13 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
}
else
{
- BOOL isImageGIF = [self.imageData sd_isGIF];
- UIImage *image;
- if (isImageGIF)
- {
- image = [UIImage sd_animatedGIFWithData:self.imageData];
- }
- else
- {
- image = [[UIImage alloc] initWithData:self.imageData];
- }
+ UIImage *image = [UIImage sd_imageWithData:self.imageData];
image = [self scaledImageForKey:self.request.URL.absoluteString image:image];
- if (!isImageGIF) {
+ if (!image.images) // Do not force decod animated GIFs
+ {
image = [UIImage decodedImageWithImage:image];
}
@@ -156,12 +156,12 @@ - (NSString *)cacheKeyForURL:(NSURL *)url
{
// Image refresh hit the NSURLCache cache, do not call the completion block
}
- else if (downloadedImage && [self.delegate respondsToSelector:@selector(imageManager:transformDownloadedImage:withURL:)])
+ // NOTE: We don't call transformDownloadedImage delegate method on animated images as most transformation code would mangle it
+ else if (downloadedImage && !downloadedImage.images && [self.delegate respondsToSelector:@selector(imageManager:transformDownloadedImage:withURL:)])
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^
- {
- BOOL isImageGIF = [data sd_isGIF];
- UIImage *transformedImage = isImageGIF ? downloadedImage : [self.delegate imageManager:self transformDownloadedImage:downloadedImage withURL:url];
+ {
+ UIImage *transformedImage = [self.delegate imageManager:self transformDownloadedImage:downloadedImage withURL:url];
dispatch_async(dispatch_get_main_queue(), ^
{
View
@@ -21,30 +21,42 @@ + (UIImage *)sd_animatedGIFWithData:(NSData *)data
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
size_t count = CGImageSourceGetCount(source);
- NSMutableArray *images = [NSMutableArray array];
-
- NSTimeInterval duration = 0.0f;
-
- for (size_t i = 0; i < count; i++)
+
+ UIImage *animatedImage;
+
+ if (count <= 1)
{
- CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
-
- NSDictionary *frameProperties = CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source, i, NULL));
- duration += [[[frameProperties objectForKey:(NSString*)kCGImagePropertyGIFDictionary] objectForKey:(NSString*)kCGImagePropertyGIFDelayTime] doubleValue];
-
- [images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
-
- CGImageRelease(image);
+ animatedImage = [[UIImage alloc] initWithData:data];
}
-
- CFRelease(source);
-
- if (!duration)
+ else
{
- duration = (1.0f/10.0f)*count;
+ NSMutableArray *images = [NSMutableArray array];
+
+ NSTimeInterval duration = 0.0f;
+
+ for (size_t i = 0; i < count; i++)
+ {
+ CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
+
+ NSDictionary *frameProperties = CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source, i, NULL));
+ duration += [[[frameProperties objectForKey:(NSString*)kCGImagePropertyGIFDictionary] objectForKey:(NSString*)kCGImagePropertyGIFDelayTime] doubleValue];
+
+ [images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
+
+ CGImageRelease(image);
+ }
+
+ if (!duration)
+ {
+ duration = (1.0f/10.0f)*count;
+ }
+
+ animatedImage = [UIImage animatedImageWithImages:images duration:duration];
}
-
- return [UIImage animatedImageWithImages:images duration:duration];
+
+ CFRelease(source);
+
+ return animatedImage;
}
+ (UIImage *)sd_animatedGIFNamed:(NSString *)name
@@ -0,0 +1,15 @@
+//
+// UIImage+MultiFormat.h
+// SDWebImage
+//
+// Created by Olivier Poitrey on 07/06/13.
+// Copyright (c) 2013 Dailymotion. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@interface UIImage (MultiFormat)
+
++ (UIImage *)sd_imageWithData:(NSData *)data;
+
+@end
@@ -0,0 +1,30 @@
+//
+// UIImage+MultiFormat.m
+// SDWebImage
+//
+// Created by Olivier Poitrey on 07/06/13.
+// Copyright (c) 2013 Dailymotion. All rights reserved.
+//
+
+#import "UIImage+MultiFormat.h"
+#import "UIImage+GIF.h"
+
+@implementation UIImage (MultiFormat)
+
++ (UIImage *)sd_imageWithData:(NSData *)data
+{
+ UIImage *image;
+
+ if ([data sd_isGIF])
+ {
+ image = [UIImage sd_animatedGIFWithData:data];
+ }
+ else
+ {
+ image = [[UIImage alloc] initWithData:data];
+ }
+
+ return image;
+}
+
+@end

0 comments on commit 1dbb79f

Please sign in to comment.