Merge pull request #375 from lavoy/master

Animated GIF Support
This commit is contained in:
Olivier Poitrey 2013-04-28 15:51:36 -07:00
commit 5db0d00ef3
9 changed files with 246 additions and 9 deletions

View File

@ -30,7 +30,7 @@
target:self target:self
action:@selector(flushCache)]; action:@selector(flushCache)];
_objects = [NSArray arrayWithObjects: _objects = [NSArray arrayWithObjects:
@"https://graph.facebook.com/olivier.poitrey/picture?height=200&width=200", @"http://assets.sbnation.com/assets/2512203/dogflops.gif",
@"http://static2.dmcdn.net/static/video/656/177/44771656:jpeg_preview_small.jpg?20120509154705", @"http://static2.dmcdn.net/static/video/656/177/44771656:jpeg_preview_small.jpg?20120509154705",
@"http://static2.dmcdn.net/static/video/629/228/44822926:jpeg_preview_small.jpg?20120509181018", @"http://static2.dmcdn.net/static/video/629/228/44822926:jpeg_preview_small.jpg?20120509181018",
@"http://static2.dmcdn.net/static/video/116/367/44763611:jpeg_preview_small.jpg?20120509101749", @"http://static2.dmcdn.net/static/video/116/367/44763611:jpeg_preview_small.jpg?20120509101749",

View File

@ -67,7 +67,14 @@
5376131E155AD0D5005750A4 /* SDWebImagePrefetcher.h in Headers */ = {isa = PBXBuildFile; fileRef = 53922D91148C56230056699D /* SDWebImagePrefetcher.h */; settings = {ATTRIBUTES = (Public, ); }; }; 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, ); }; }; 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, ); }; }; 53761320155AD0D5005750A4 /* UIImageView+WebCache.h in Headers */ = {isa = PBXBuildFile; fileRef = 53922D95148C56230056699D /* UIImageView+WebCache.h */; settings = {ATTRIBUTES = (Public, ); }; };
A99C65A116F9CA2600A73D75 /* SDWebImageCompat.m in Sources */ = {isa = PBXBuildFile; fileRef = 5340674F167780C40042B59E /* SDWebImageCompat.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 */; };
A18A6CCA172DC28500419892 /* UIImage+GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = A18A6CC6172DC28500419892 /* UIImage+GIF.m */; };
A18A6CCD172DC33A00419892 /* NSData+GIF.h in Headers */ = {isa = PBXBuildFile; fileRef = A18A6CCB172DC33A00419892 /* NSData+GIF.h */; };
A18A6CCE172DC33A00419892 /* NSData+GIF.h in Headers */ = {isa = PBXBuildFile; fileRef = A18A6CCB172DC33A00419892 /* NSData+GIF.h */; };
A18A6CCF172DC33A00419892 /* NSData+GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = A18A6CCC172DC33A00419892 /* NSData+GIF.m */; };
A18A6CD0172DC33A00419892 /* NSData+GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = A18A6CCC172DC33A00419892 /* NSData+GIF.m */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */ /* Begin PBXContainerItemProxy section */
@ -107,6 +114,10 @@
53922D96148C56230056699D /* UIImageView+WebCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIImageView+WebCache.m"; path = "SDWebImage/UIImageView+WebCache.m"; 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; };
53FB893F14D35D1A0020B787 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; }; 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; }; 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>"; };
A18A6CC6172DC28500419892 /* UIImage+GIF.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+GIF.m"; sourceTree = "<group>"; };
A18A6CCB172DC33A00419892 /* NSData+GIF.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+GIF.h"; sourceTree = "<group>"; };
A18A6CCC172DC33A00419892 /* NSData+GIF.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSData+GIF.m"; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
@ -181,8 +192,12 @@
children = ( children = (
535699B415113E7300A4C397 /* MKAnnotationView+WebCache.h */, 535699B415113E7300A4C397 /* MKAnnotationView+WebCache.h */,
535699B515113E7300A4C397 /* MKAnnotationView+WebCache.m */, 535699B515113E7300A4C397 /* MKAnnotationView+WebCache.m */,
A18A6CCB172DC33A00419892 /* NSData+GIF.h */,
A18A6CCC172DC33A00419892 /* NSData+GIF.m */,
53922D93148C56230056699D /* UIButton+WebCache.h */, 53922D93148C56230056699D /* UIButton+WebCache.h */,
53922D94148C56230056699D /* UIButton+WebCache.m */, 53922D94148C56230056699D /* UIButton+WebCache.m */,
A18A6CC5172DC28500419892 /* UIImage+GIF.h */,
A18A6CC6172DC28500419892 /* UIImage+GIF.m */,
53922D95148C56230056699D /* UIImageView+WebCache.h */, 53922D95148C56230056699D /* UIImageView+WebCache.h */,
53922D96148C56230056699D /* UIImageView+WebCache.m */, 53922D96148C56230056699D /* UIImageView+WebCache.m */,
); );
@ -240,6 +255,8 @@
531041DC157EAFA400BBABC3 /* MKAnnotationView+WebCache.h in Headers */, 531041DC157EAFA400BBABC3 /* MKAnnotationView+WebCache.h in Headers */,
530E49E916464C26002868E7 /* SDWebImageOperation.h in Headers */, 530E49E916464C26002868E7 /* SDWebImageOperation.h in Headers */,
530E49EB16464C7F002868E7 /* SDWebImageDownloaderOperation.h in Headers */, 530E49EB16464C7F002868E7 /* SDWebImageDownloaderOperation.h in Headers */,
A18A6CC8172DC28500419892 /* UIImage+GIF.h in Headers */,
A18A6CCE172DC33A00419892 /* NSData+GIF.h in Headers */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
@ -257,6 +274,8 @@
53761320155AD0D5005750A4 /* UIImageView+WebCache.h in Headers */, 53761320155AD0D5005750A4 /* UIImageView+WebCache.h in Headers */,
530E49E816464C25002868E7 /* SDWebImageOperation.h in Headers */, 530E49E816464C25002868E7 /* SDWebImageOperation.h in Headers */,
530E49EA16464C7C002868E7 /* SDWebImageDownloaderOperation.h in Headers */, 530E49EA16464C7C002868E7 /* SDWebImageDownloaderOperation.h in Headers */,
A18A6CC7172DC28500419892 /* UIImage+GIF.h in Headers */,
A18A6CCD172DC33A00419892 /* NSData+GIF.h in Headers */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
@ -362,7 +381,6 @@
isa = PBXSourcesBuildPhase; isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
A99C65A116F9CA2600A73D75 /* SDWebImageCompat.m in Sources */,
531041C4157EAFA400BBABC3 /* SDImageCache.m in Sources */, 531041C4157EAFA400BBABC3 /* SDImageCache.m in Sources */,
531041C5157EAFA400BBABC3 /* SDWebImageDecoder.m in Sources */, 531041C5157EAFA400BBABC3 /* SDWebImageDecoder.m in Sources */,
531041C6157EAFA400BBABC3 /* SDWebImageDownloader.m in Sources */, 531041C6157EAFA400BBABC3 /* SDWebImageDownloader.m in Sources */,
@ -372,6 +390,8 @@
531041CA157EAFA400BBABC3 /* UIImageView+WebCache.m in Sources */, 531041CA157EAFA400BBABC3 /* UIImageView+WebCache.m in Sources */,
531041CB157EAFA400BBABC3 /* MKAnnotationView+WebCache.m in Sources */, 531041CB157EAFA400BBABC3 /* MKAnnotationView+WebCache.m in Sources */,
530E49ED16464C84002868E7 /* SDWebImageDownloaderOperation.m in Sources */, 530E49ED16464C84002868E7 /* SDWebImageDownloaderOperation.m in Sources */,
A18A6CCA172DC28500419892 /* UIImage+GIF.m in Sources */,
A18A6CD0172DC33A00419892 /* NSData+GIF.m in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
@ -388,6 +408,8 @@
5376130F155AD0D5005750A4 /* UIImageView+WebCache.m in Sources */, 5376130F155AD0D5005750A4 /* UIImageView+WebCache.m in Sources */,
530E49EC16464C84002868E7 /* SDWebImageDownloaderOperation.m in Sources */, 530E49EC16464C84002868E7 /* SDWebImageDownloaderOperation.m in Sources */,
53406750167780C40042B59E /* SDWebImageCompat.m in Sources */, 53406750167780C40042B59E /* SDWebImageCompat.m in Sources */,
A18A6CC9172DC28500419892 /* UIImage+GIF.m in Sources */,
A18A6CCF172DC33A00419892 /* NSData+GIF.m in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };

15
SDWebImage/NSData+GIF.h Normal file
View File

@ -0,0 +1,15 @@
//
// NSData+GIF.h
// SDWebImage
//
// Created by Andy LaVoy on 4/28/13.
// Copyright (c) 2013 Dailymotion. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSData (GIF)
- (BOOL)isGIF;
@end

32
SDWebImage/NSData+GIF.m Normal file
View File

@ -0,0 +1,32 @@
//
// NSData+GIF.m
// SDWebImage
//
// Created by Andy LaVoy on 4/28/13.
// Copyright (c) 2013 Dailymotion. All rights reserved.
//
#import "NSData+GIF.h"
@implementation NSData (GIF)
- (BOOL)isGIF
{
BOOL isGIF = NO;
uint8_t c;
[self getBytes:&c length:1];
switch (c)
{
case 0x47: // probably a GIF
isGIF = YES;
break;
default:
break;
}
return isGIF;
}
@end

View File

@ -8,6 +8,7 @@
#import "SDImageCache.h" #import "SDImageCache.h"
#import "SDWebImageDecoder.h" #import "SDWebImageDecoder.h"
#import "UIImage+GIF.h"
#import <CommonCrypto/CommonDigest.h> #import <CommonCrypto/CommonDigest.h>
#import <mach/mach.h> #import <mach/mach.h>
#import <mach/mach_host.h> #import <mach/mach_host.h>
@ -180,9 +181,16 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
NSData *data = [NSData dataWithContentsOfFile:path]; NSData *data = [NSData dataWithContentsOfFile:path];
if (data) if (data)
{ {
UIImage *image = [[UIImage alloc] initWithData:data]; if ([data isGIF])
UIImage *scaledImage = [self scaledImageForKey:key image:image]; {
return [UIImage decodedImageWithImage:scaledImage]; return [UIImage animatedGIFWithData:data];
}
else
{
UIImage *image = [[UIImage alloc] initWithData:data];
UIImage *scaledImage = [self scaledImageForKey:key image:image];
return [UIImage decodedImageWithImage:scaledImage];
}
} }
else else
{ {

View File

@ -37,4 +37,4 @@
#define SDDispatchQueueSetterSementics assign #define SDDispatchQueueSetterSementics assign
#endif #endif
extern inline UIImage *SDScaledImageForKey(NSString *key, UIImage *image); extern inline UIImage *SDScaledImageForKey(NSString *key, UIImage *image);

View File

@ -7,6 +7,7 @@
*/ */
#import "SDWebImageManager.h" #import "SDWebImageManager.h"
#import "UIImage+GIF.h"
#import <objc/message.h> #import <objc/message.h>
@interface SDWebImageCombinedOperation : NSObject <SDWebImageOperation> @interface SDWebImageCombinedOperation : NSObject <SDWebImageOperation>
@ -121,6 +122,12 @@
} }
__block id<SDWebImageOperation> subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished) __block id<SDWebImageOperation> subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished)
{ {
BOOL isImageGIF = [data isGIF];
if (isImageGIF)
{
downloadedImage = [UIImage animatedGIFWithData:data];
}
if (weakOperation.cancelled) if (weakOperation.cancelled)
{ {
completedBlock(nil, nil, SDImageCacheTypeNone, finished); completedBlock(nil, nil, SDImageCacheTypeNone, finished);
@ -149,7 +156,7 @@
{ {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^
{ {
UIImage *transformedImage = [self.delegate imageManager:self transformDownloadedImage:downloadedImage withURL:url]; UIImage *transformedImage = isImageGIF ? downloadedImage : [self.delegate imageManager:self transformDownloadedImage:downloadedImage withURL:url];
dispatch_async(dispatch_get_main_queue(), ^ dispatch_async(dispatch_get_main_queue(), ^
{ {
@ -158,7 +165,8 @@
if (transformedImage && finished) if (transformedImage && finished)
{ {
[self.imageCache storeImage:transformedImage imageData:nil forKey:key toDisk:cacheOnDisk]; NSData *dataToStore = isImageGIF ? data : nil;
[self.imageCache storeImage:transformedImage imageData:dataToStore forKey:key toDisk:cacheOnDisk];
} }
}); });
} }

19
SDWebImage/UIImage+GIF.h Executable file
View File

@ -0,0 +1,19 @@
//
// UIImage+GIF.h
// LBGIFImage
//
// Created by Laurin Brandner on 06.01.12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "NSData+GIF.h"
#import <UIKit/UIKit.h>
@interface UIImage (GIF)
+ (UIImage *)animatedGIFNamed:(NSString *)name;
+ (UIImage *)animatedGIFWithData:(NSData *)data;
- (UIImage *)animatedImageByScalingAndCroppingToSize:(CGSize)size;
@end

133
SDWebImage/UIImage+GIF.m Executable file
View File

@ -0,0 +1,133 @@
//
// UIImage+GIF.m
// LBGIFImage
//
// Created by Laurin Brandner on 06.01.12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "UIImage+GIF.h"
#import <ImageIO/ImageIO.h>
@implementation UIImage (GIF)
+ (UIImage *)animatedGIFWithData:(NSData *)data
{
if (!data)
{
return nil;
}
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++)
{
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);
}
CFRelease(source);
if (!duration)
{
duration = (1.0f/10.0f)*count;
}
return [UIImage animatedImageWithImages:images duration:duration];
}
+ (UIImage *)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 animatedGIFWithData:data];
}
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
data = [NSData dataWithContentsOfFile:path];
if (data)
{
return [UIImage animatedGIFWithData:data];
}
return [UIImage imageNamed:name];
}
else
{
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
NSData *data = [NSData dataWithContentsOfFile:path];
if (data)
{
return [UIImage animatedGIFWithData:data];
}
return [UIImage imageNamed:name];
}
}
- (UIImage *)animatedImageByScalingAndCroppingToSize:(CGSize)size
{
if (CGSizeEqualToSize(self.size, size) || CGSizeEqualToSize(size, CGSizeZero))
{
return self;
}
CGSize scaledSize = size;
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;
}
else if (widthFactor < heightFactor)
{
thumbnailPoint.x = (size.width - scaledSize.width) * 0.5;
}
NSMutableArray *scaledImages = [NSMutableArray array];
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];
}
@end