updated formatting for project

This commit is contained in:
Andy LaVoy 2013-04-28 14:59:33 -07:00
parent 5bf37d5472
commit 2a370b062b
5 changed files with 54 additions and 31 deletions

View File

@ -10,13 +10,15 @@
@implementation NSData (GIF) @implementation NSData (GIF)
- (BOOL)isGIF { - (BOOL)isGIF
{
BOOL isGIF = NO; BOOL isGIF = NO;
uint8_t c; uint8_t c;
[self getBytes:&c length:1]; [self getBytes:&c length:1];
switch (c) { switch (c)
{
case 0x47: // probably a GIF case 0x47: // probably a GIF
isGIF = YES; isGIF = YES;
break; break;

View File

@ -181,9 +181,12 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
NSData *data = [NSData dataWithContentsOfFile:path]; NSData *data = [NSData dataWithContentsOfFile:path];
if (data) if (data)
{ {
if ([data isGIF]) { if ([data isGIF])
{
return [UIImage animatedGIFWithData:data]; return [UIImage animatedGIFWithData:data];
} else { }
else
{
UIImage *image = [[UIImage alloc] initWithData:data]; UIImage *image = [[UIImage alloc] initWithData:data];
UIImage *scaledImage = [self scaledImageForKey:key image:image]; UIImage *scaledImage = [self scaledImageForKey:key image:image];
return [UIImage decodedImageWithImage:scaledImage]; return [UIImage decodedImageWithImage:scaledImage];

View File

@ -123,7 +123,8 @@
__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 imageIsGIF = [data isGIF]; BOOL imageIsGIF = [data isGIF];
if (imageIsGIF) { if (imageIsGIF)
{
downloadedImage = [UIImage animatedGIFWithData:data]; downloadedImage = [UIImage animatedGIFWithData:data];
} }

View File

@ -11,9 +11,9 @@
@interface UIImage (GIF) @interface UIImage (GIF)
+(UIImage*)animatedGIFNamed:(NSString*)name; + (UIImage *)animatedGIFNamed:(NSString *)name;
+(UIImage*)animatedGIFWithData:(NSData *)data; + (UIImage *)animatedGIFWithData:(NSData *)data;
-(UIImage*)animatedImageByScalingAndCroppingToSize:(CGSize)size; - (UIImage *)animatedImageByScalingAndCroppingToSize:(CGSize)size;
@end @end

View File

@ -11,22 +11,25 @@
@implementation UIImage (GIF) @implementation UIImage (GIF)
+(UIImage*)animatedGIFWithData:(NSData *)data { + (UIImage *)animatedGIFWithData:(NSData *)data
if (!data) { {
if (!data)
{
return nil; return nil;
} }
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
size_t count = CGImageSourceGetCount(source); size_t count = CGImageSourceGetCount(source);
NSMutableArray* images = [NSMutableArray array]; NSMutableArray *images = [NSMutableArray array];
NSTimeInterval duration = 0.0f; 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); 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]; duration += [[[frameProperties objectForKey:(NSString*)kCGImagePropertyGIFDictionary] objectForKey:(NSString*)kCGImagePropertyGIFDelayTime] doubleValue];
[images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]]; [images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
@ -36,41 +39,48 @@
CFRelease(source); CFRelease(source);
if (!duration) { if (!duration)
{
duration = (1.0f/10.0f)*count; duration = (1.0f/10.0f)*count;
} }
return [UIImage animatedImageWithImages:images duration:duration]; return [UIImage animatedImageWithImages:images duration:duration];
} }
+(UIImage*)animatedGIFNamed:(NSString *)name { + (UIImage *)animatedGIFNamed:(NSString *)name
{
CGFloat scale = [UIScreen mainScreen].scale; CGFloat scale = [UIScreen mainScreen].scale;
if (scale > 1.0f) { if (scale > 1.0f)
NSString* retinaPath = [[NSBundle mainBundle] pathForResource:[name stringByAppendingString:@"@2x"] ofType:@"gif"]; {
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]; return [UIImage animatedGIFWithData:data];
} }
NSString* path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"]; NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
data = [NSData dataWithContentsOfFile:path]; data = [NSData dataWithContentsOfFile:path];
if (data) { if (data)
{
return [UIImage animatedGIFWithData:data]; return [UIImage animatedGIFWithData:data];
} }
return [UIImage imageNamed:name]; return [UIImage imageNamed:name];
} }
else { else
NSString* path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"]; {
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]; return [UIImage animatedGIFWithData:data];
} }
@ -78,8 +88,10 @@
} }
} }
-(UIImage*)animatedImageByScalingAndCroppingToSize:(CGSize)size { - (UIImage *)animatedImageByScalingAndCroppingToSize:(CGSize)size
if (CGSizeEqualToSize(self.size, size) || CGSizeEqualToSize(size, CGSizeZero)) { {
if (CGSizeEqualToSize(self.size, size) || CGSizeEqualToSize(size, CGSizeZero))
{
return self; return self;
} }
@ -91,17 +103,22 @@
CGFloat scaleFactor = (widthFactor > heightFactor) ? widthFactor :heightFactor; CGFloat scaleFactor = (widthFactor > heightFactor) ? widthFactor :heightFactor;
scaledSize.width = self.size.width * scaleFactor; scaledSize.width = self.size.width * scaleFactor;
scaledSize.height = self.size.height * scaleFactor; scaledSize.height = self.size.height * scaleFactor;
if (widthFactor > heightFactor) {
if (widthFactor > heightFactor)
{
thumbnailPoint.y = (size.height - scaledSize.height) * 0.5; thumbnailPoint.y = (size.height - scaledSize.height) * 0.5;
} else if (widthFactor < heightFactor) { }
else if (widthFactor < heightFactor)
{
thumbnailPoint.x = (size.width - scaledSize.width) * 0.5; thumbnailPoint.x = (size.width - scaledSize.width) * 0.5;
} }
NSMutableArray* scaledImages = [NSMutableArray array]; NSMutableArray *scaledImages = [NSMutableArray array];
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); 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)]; [image drawInRect:CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledSize.width, scaledSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();