From 5bf37d5472adfda6319af096a0eb36c3a2e1ea81 Mon Sep 17 00:00:00 2001 From: Andy LaVoy Date: Sun, 28 Apr 2013 14:37:47 -0700 Subject: [PATCH] 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]; } }); }