support GIFs

This commit is contained in:
Andy LaVoy 2013-04-28 14:06:54 -07:00
parent 17d04b70e5
commit 3f98095704
8 changed files with 211 additions and 4 deletions

View File

@ -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",

View File

@ -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 = "<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 */
/* 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;
};

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

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

@ -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

View File

@ -37,4 +37,4 @@
#define SDDispatchQueueSetterSementics assign
#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 "UIImage+GIF.h"
#import <objc/message.h>
@interface SDWebImageCombinedOperation : NSObject <SDWebImageOperation>
@ -121,6 +122,10 @@
}
__block id<SDWebImageOperation> 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);

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

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

@ -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 <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