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();