From 3f980957043050a0fa6091da451f0e0e2f09126a Mon Sep 17 00:00:00 2001 From: Andy LaVoy Date: Sun, 28 Apr 2013 14:06:54 -0700 Subject: [PATCH 1/4] support GIFs --- .../SDWebImage Demo/MasterViewController.m | 2 +- SDWebImage.xcodeproj/project.pbxproj | 26 +++- SDWebImage/NSData+GIF.h | 15 +++ SDWebImage/NSData+GIF.m | 30 +++++ SDWebImage/SDWebImageCompat.h | 2 +- SDWebImage/SDWebImageManager.m | 5 + SDWebImage/UIImage+GIF.h | 19 +++ SDWebImage/UIImage+GIF.m | 116 ++++++++++++++++++ 8 files changed, 211 insertions(+), 4 deletions(-) create mode 100644 SDWebImage/NSData+GIF.h create mode 100644 SDWebImage/NSData+GIF.m create mode 100755 SDWebImage/UIImage+GIF.h create mode 100755 SDWebImage/UIImage+GIF.m diff --git a/Examples/SDWebImage Demo/MasterViewController.m b/Examples/SDWebImage Demo/MasterViewController.m index 4fd6ce3e..7faae738 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 749ce853..f74b9928 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 00000000..556d7a27 --- /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 00000000..ae445db9 --- /dev/null +++ b/SDWebImage/NSData+GIF.m @@ -0,0 +1,30 @@ +// +// 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/SDWebImageCompat.h b/SDWebImage/SDWebImageCompat.h index b14b8cb8..66f5e6e7 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 8f251261..023c5c8c 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,10 @@ } __block id subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished) { + if ([data isGIF]) { + downloadedImage = [UIImage animatedGIFWithData:data]; + } + if (weakOperation.cancelled) { completedBlock(nil, nil, SDImageCacheTypeNone, finished); diff --git a/SDWebImage/UIImage+GIF.h b/SDWebImage/UIImage+GIF.h new file mode 100755 index 00000000..3fccf77d --- /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 00000000..83b3b09f --- /dev/null +++ b/SDWebImage/UIImage+GIF.m @@ -0,0 +1,116 @@ +// +// 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 From 5bf37d5472adfda6319af096a0eb36c3a2e1ea81 Mon Sep 17 00:00:00 2001 From: Andy LaVoy Date: Sun, 28 Apr 2013 14:37:47 -0700 Subject: [PATCH 2/4] cache the GIF data instead of the jpeg representation --- SDWebImage/SDImageCache.m | 11 ++++++++--- SDWebImage/SDWebImageManager.m | 8 +++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 00e776ed..3440d7da 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,13 @@ 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/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index 023c5c8c..a207dc3f 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -122,7 +122,8 @@ } __block id subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished) { - if ([data isGIF]) { + BOOL imageIsGIF = [data isGIF]; + if (imageIsGIF) { downloadedImage = [UIImage animatedGIFWithData:data]; } @@ -154,7 +155,7 @@ { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ { - UIImage *transformedImage = [self.delegate imageManager:self transformDownloadedImage:downloadedImage withURL:url]; + UIImage *transformedImage = imageIsGIF ? downloadedImage : [self.delegate imageManager:self transformDownloadedImage:downloadedImage withURL:url]; dispatch_async(dispatch_get_main_queue(), ^ { @@ -163,7 +164,8 @@ if (transformedImage && finished) { - [self.imageCache storeImage:transformedImage imageData:nil forKey:key toDisk:cacheOnDisk]; + NSData *dataToStore = imageIsGIF ? data : nil; + [self.imageCache storeImage:transformedImage imageData:dataToStore forKey:key toDisk:cacheOnDisk]; } }); } From 2a370b062b090092463f58fdfb87c20934f06ae1 Mon Sep 17 00:00:00 2001 From: Andy LaVoy Date: Sun, 28 Apr 2013 14:59:33 -0700 Subject: [PATCH 3/4] updated formatting for project --- SDWebImage/NSData+GIF.m | 6 ++-- SDWebImage/SDImageCache.m | 7 ++-- SDWebImage/SDWebImageManager.m | 3 +- SDWebImage/UIImage+GIF.h | 6 ++-- SDWebImage/UIImage+GIF.m | 63 +++++++++++++++++++++------------- 5 files changed, 54 insertions(+), 31 deletions(-) diff --git a/SDWebImage/NSData+GIF.m b/SDWebImage/NSData+GIF.m index ae445db9..3ed233cf 100644 --- a/SDWebImage/NSData+GIF.m +++ b/SDWebImage/NSData+GIF.m @@ -10,13 +10,15 @@ @implementation NSData (GIF) -- (BOOL)isGIF { +- (BOOL)isGIF +{ BOOL isGIF = NO; uint8_t c; [self getBytes:&c length:1]; - switch (c) { + switch (c) + { case 0x47: // probably a GIF isGIF = YES; break; diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 3440d7da..6746a370 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -181,9 +181,12 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week NSData *data = [NSData dataWithContentsOfFile:path]; if (data) { - if ([data isGIF]) { + if ([data isGIF]) + { return [UIImage animatedGIFWithData:data]; - } else { + } + else + { UIImage *image = [[UIImage alloc] initWithData:data]; UIImage *scaledImage = [self scaledImageForKey:key image:image]; return [UIImage decodedImageWithImage:scaledImage]; diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index a207dc3f..f4f98832 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -123,7 +123,8 @@ __block id subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished) { BOOL imageIsGIF = [data isGIF]; - if (imageIsGIF) { + if (imageIsGIF) + { downloadedImage = [UIImage animatedGIFWithData:data]; } diff --git a/SDWebImage/UIImage+GIF.h b/SDWebImage/UIImage+GIF.h index 3fccf77d..3006de58 100755 --- a/SDWebImage/UIImage+GIF.h +++ b/SDWebImage/UIImage+GIF.h @@ -11,9 +11,9 @@ @interface UIImage (GIF) -+(UIImage*)animatedGIFNamed:(NSString*)name; -+(UIImage*)animatedGIFWithData:(NSData *)data; ++ (UIImage *)animatedGIFNamed:(NSString *)name; ++ (UIImage *)animatedGIFWithData:(NSData *)data; --(UIImage*)animatedImageByScalingAndCroppingToSize:(CGSize)size; +- (UIImage *)animatedImageByScalingAndCroppingToSize:(CGSize)size; @end diff --git a/SDWebImage/UIImage+GIF.m b/SDWebImage/UIImage+GIF.m index 83b3b09f..8a0cc61d 100755 --- a/SDWebImage/UIImage+GIF.m +++ b/SDWebImage/UIImage+GIF.m @@ -11,22 +11,25 @@ @implementation UIImage (GIF) -+(UIImage*)animatedGIFWithData:(NSData *)data { - if (!data) { ++ (UIImage *)animatedGIFWithData:(NSData *)data +{ + if (!data) + { return nil; } CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); size_t count = CGImageSourceGetCount(source); - NSMutableArray* images = [NSMutableArray array]; + NSMutableArray *images = [NSMutableArray array]; NSTimeInterval duration = 0.0f; - for (size_t i = 0; i < count; i++) { + for (size_t i = 0; i < count; i++) + { CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL); - NSDictionary* frameProperties = CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(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]]; @@ -36,41 +39,48 @@ CFRelease(source); - if (!duration) { + if (!duration) + { duration = (1.0f/10.0f)*count; } return [UIImage animatedImageWithImages:images duration:duration]; } -+(UIImage*)animatedGIFNamed:(NSString *)name { ++ (UIImage *)animatedGIFNamed:(NSString *)name +{ CGFloat scale = [UIScreen mainScreen].scale; - if (scale > 1.0f) { - NSString* retinaPath = [[NSBundle mainBundle] pathForResource:[name stringByAppendingString:@"@2x"] ofType:@"gif"]; + if (scale > 1.0f) + { + NSString *retinaPath = [[NSBundle mainBundle] pathForResource:[name stringByAppendingString:@"@2x"] ofType:@"gif"]; - NSData* data = [NSData dataWithContentsOfFile:retinaPath]; + NSData *data = [NSData dataWithContentsOfFile:retinaPath]; - if (data) { + if (data) + { return [UIImage animatedGIFWithData:data]; } - NSString* path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"]; + NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"]; data = [NSData dataWithContentsOfFile:path]; - if (data) { + if (data) + { return [UIImage animatedGIFWithData:data]; } return [UIImage imageNamed:name]; } - else { - NSString* path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"]; + else + { + NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"]; - NSData* data = [NSData dataWithContentsOfFile:path]; + NSData *data = [NSData dataWithContentsOfFile:path]; - if (data) { + if (data) + { return [UIImage animatedGIFWithData:data]; } @@ -78,8 +88,10 @@ } } --(UIImage*)animatedImageByScalingAndCroppingToSize:(CGSize)size { - if (CGSizeEqualToSize(self.size, size) || CGSizeEqualToSize(size, CGSizeZero)) { +- (UIImage *)animatedImageByScalingAndCroppingToSize:(CGSize)size +{ + if (CGSizeEqualToSize(self.size, size) || CGSizeEqualToSize(size, CGSizeZero)) + { return self; } @@ -91,17 +103,22 @@ CGFloat scaleFactor = (widthFactor > heightFactor) ? widthFactor :heightFactor; scaledSize.width = self.size.width * scaleFactor; scaledSize.height = self.size.height * scaleFactor; - if (widthFactor > heightFactor) { + + if (widthFactor > heightFactor) + { thumbnailPoint.y = (size.height - scaledSize.height) * 0.5; - } else if (widthFactor < heightFactor) { + } + else if (widthFactor < heightFactor) + { thumbnailPoint.x = (size.width - scaledSize.width) * 0.5; } - NSMutableArray* scaledImages = [NSMutableArray array]; + NSMutableArray *scaledImages = [NSMutableArray array]; UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); - for (UIImage* image in self.images) { + for (UIImage *image in self.images) + { [image drawInRect:CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledSize.width, scaledSize.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); From d789fd92e8580474bf250289e841dfac647c8d9b Mon Sep 17 00:00:00 2001 From: Andy LaVoy Date: Sun, 28 Apr 2013 15:38:50 -0700 Subject: [PATCH 4/4] change var name --- SDWebImage/SDWebImageManager.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index f4f98832..3c83301d 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -122,8 +122,8 @@ } __block id subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished) { - BOOL imageIsGIF = [data isGIF]; - if (imageIsGIF) + BOOL isImageGIF = [data isGIF]; + if (isImageGIF) { downloadedImage = [UIImage animatedGIFWithData:data]; } @@ -156,7 +156,7 @@ { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ { - UIImage *transformedImage = imageIsGIF ? downloadedImage : [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(), ^ { @@ -165,7 +165,7 @@ if (transformedImage && finished) { - NSData *dataToStore = imageIsGIF ? data : nil; + NSData *dataToStore = isImageGIF ? data : nil; [self.imageCache storeImage:transformedImage imageData:dataToStore forKey:key toDisk:cacheOnDisk]; } });