diff --git a/Examples/SDWebImage Demo/MasterViewController.m b/Examples/SDWebImage Demo/MasterViewController.m index 4fd6ce3..7faae73 100644 --- a/Examples/SDWebImage Demo/MasterViewController.m +++ b/Examples/SDWebImage Demo/MasterViewController.m @@ -30,7 +30,7 @@ target:self action:@selector(flushCache)]; _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/629/228/44822926:jpeg_preview_small.jpg?20120509181018", @"http://static2.dmcdn.net/static/video/116/367/44763611:jpeg_preview_small.jpg?20120509101749", diff --git a/SDWebImage.xcodeproj/project.pbxproj b/SDWebImage.xcodeproj/project.pbxproj index 749ce85..f74b992 100644 --- a/SDWebImage.xcodeproj/project.pbxproj +++ b/SDWebImage.xcodeproj/project.pbxproj @@ -67,7 +67,14 @@ 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, ); }; }; - 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 */ /* 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; }; 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 = ""; }; + A18A6CC6172DC28500419892 /* UIImage+GIF.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+GIF.m"; sourceTree = ""; }; + A18A6CCB172DC33A00419892 /* NSData+GIF.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+GIF.h"; sourceTree = ""; }; + A18A6CCC172DC33A00419892 /* NSData+GIF.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSData+GIF.m"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -181,8 +192,12 @@ children = ( 535699B415113E7300A4C397 /* MKAnnotationView+WebCache.h */, 535699B515113E7300A4C397 /* MKAnnotationView+WebCache.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 */, 53922D95148C56230056699D /* UIImageView+WebCache.h */, 53922D96148C56230056699D /* UIImageView+WebCache.m */, ); @@ -240,6 +255,8 @@ 531041DC157EAFA400BBABC3 /* MKAnnotationView+WebCache.h in Headers */, 530E49E916464C26002868E7 /* SDWebImageOperation.h in Headers */, 530E49EB16464C7F002868E7 /* SDWebImageDownloaderOperation.h in Headers */, + A18A6CC8172DC28500419892 /* UIImage+GIF.h in Headers */, + A18A6CCE172DC33A00419892 /* NSData+GIF.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -257,6 +274,8 @@ 53761320155AD0D5005750A4 /* UIImageView+WebCache.h in Headers */, 530E49E816464C25002868E7 /* SDWebImageOperation.h in Headers */, 530E49EA16464C7C002868E7 /* SDWebImageDownloaderOperation.h in Headers */, + A18A6CC7172DC28500419892 /* UIImage+GIF.h in Headers */, + A18A6CCD172DC33A00419892 /* NSData+GIF.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -362,7 +381,6 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - A99C65A116F9CA2600A73D75 /* SDWebImageCompat.m in Sources */, 531041C4157EAFA400BBABC3 /* SDImageCache.m in Sources */, 531041C5157EAFA400BBABC3 /* SDWebImageDecoder.m in Sources */, 531041C6157EAFA400BBABC3 /* SDWebImageDownloader.m in Sources */, @@ -372,6 +390,8 @@ 531041CA157EAFA400BBABC3 /* UIImageView+WebCache.m in Sources */, 531041CB157EAFA400BBABC3 /* MKAnnotationView+WebCache.m in Sources */, 530E49ED16464C84002868E7 /* SDWebImageDownloaderOperation.m in Sources */, + A18A6CCA172DC28500419892 /* UIImage+GIF.m in Sources */, + A18A6CD0172DC33A00419892 /* NSData+GIF.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -388,6 +408,8 @@ 5376130F155AD0D5005750A4 /* UIImageView+WebCache.m in Sources */, 530E49EC16464C84002868E7 /* SDWebImageDownloaderOperation.m in Sources */, 53406750167780C40042B59E /* SDWebImageCompat.m in Sources */, + A18A6CC9172DC28500419892 /* UIImage+GIF.m in Sources */, + A18A6CCF172DC33A00419892 /* NSData+GIF.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/SDWebImage/NSData+GIF.h b/SDWebImage/NSData+GIF.h new file mode 100644 index 0000000..556d7a2 --- /dev/null +++ b/SDWebImage/NSData+GIF.h @@ -0,0 +1,15 @@ +// +// NSData+GIF.h +// SDWebImage +// +// Created by Andy LaVoy on 4/28/13. +// Copyright (c) 2013 Dailymotion. All rights reserved. +// + +#import + +@interface NSData (GIF) + +- (BOOL)isGIF; + +@end diff --git a/SDWebImage/NSData+GIF.m b/SDWebImage/NSData+GIF.m new file mode 100644 index 0000000..3ed233c --- /dev/null +++ b/SDWebImage/NSData+GIF.m @@ -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 diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 00e776e..6746a37 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -8,6 +8,7 @@ #import "SDImageCache.h" #import "SDWebImageDecoder.h" +#import "UIImage+GIF.h" #import #import #import @@ -180,9 +181,16 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week NSData *data = [NSData dataWithContentsOfFile:path]; if (data) { - UIImage *image = [[UIImage alloc] initWithData:data]; - UIImage *scaledImage = [self scaledImageForKey:key image:image]; - return [UIImage decodedImageWithImage:scaledImage]; + if ([data isGIF]) + { + return [UIImage animatedGIFWithData:data]; + } + else + { + UIImage *image = [[UIImage alloc] initWithData:data]; + UIImage *scaledImage = [self scaledImageForKey:key image:image]; + return [UIImage decodedImageWithImage:scaledImage]; + } } else { diff --git a/SDWebImage/SDWebImageCompat.h b/SDWebImage/SDWebImageCompat.h index b14b8cb..66f5e6e 100644 --- a/SDWebImage/SDWebImageCompat.h +++ b/SDWebImage/SDWebImageCompat.h @@ -37,4 +37,4 @@ #define SDDispatchQueueSetterSementics assign #endif -extern inline UIImage *SDScaledImageForKey(NSString *key, UIImage *image); \ No newline at end of file +extern inline UIImage *SDScaledImageForKey(NSString *key, UIImage *image); diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index 8f25126..3c83301 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -7,6 +7,7 @@ */ #import "SDWebImageManager.h" +#import "UIImage+GIF.h" #import @interface SDWebImageCombinedOperation : NSObject @@ -121,6 +122,12 @@ } __block id 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) { completedBlock(nil, nil, SDImageCacheTypeNone, finished); @@ -149,7 +156,7 @@ { 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(), ^ { @@ -158,7 +165,8 @@ 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]; } }); } diff --git a/SDWebImage/UIImage+GIF.h b/SDWebImage/UIImage+GIF.h new file mode 100755 index 0000000..3006de5 --- /dev/null +++ b/SDWebImage/UIImage+GIF.h @@ -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 + +@interface UIImage (GIF) + ++ (UIImage *)animatedGIFNamed:(NSString *)name; ++ (UIImage *)animatedGIFWithData:(NSData *)data; + +- (UIImage *)animatedImageByScalingAndCroppingToSize:(CGSize)size; + +@end diff --git a/SDWebImage/UIImage+GIF.m b/SDWebImage/UIImage+GIF.m new file mode 100755 index 0000000..8a0cc61 --- /dev/null +++ b/SDWebImage/UIImage+GIF.m @@ -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 + +@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