Changed the current internal GIF implementation. We will only return an UIImage with a single animated frame (basically a static image). We do set it in the images array so we can distinguish between those and real static images. Dropped all other GIF code.
This commit is contained in:
parent
debc0d4827
commit
9ea1602157
|
@ -10,10 +10,14 @@
|
|||
|
||||
@interface UIImage (GIF)
|
||||
|
||||
+ (UIImage *)sd_animatedGIFNamed:(NSString *)name;
|
||||
|
||||
/**
|
||||
* Compatibility method - creates an animated UIImage from an NSData, it will only contain the 1st frame image
|
||||
*/
|
||||
+ (UIImage *)sd_animatedGIFWithData:(NSData *)data;
|
||||
|
||||
- (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size;
|
||||
/**
|
||||
* Checks if an UIImage instance is a GIF. Will use the `images` array
|
||||
*/
|
||||
- (BOOL)isGIF;
|
||||
|
||||
@end
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
#import "UIImage+GIF.h"
|
||||
#import <ImageIO/ImageIO.h>
|
||||
#import "objc/runtime.h"
|
||||
|
||||
@implementation UIImage (GIF)
|
||||
|
||||
|
@ -20,142 +21,26 @@
|
|||
|
||||
size_t count = CGImageSourceGetCount(source);
|
||||
|
||||
UIImage *animatedImage;
|
||||
UIImage *staticImage;
|
||||
|
||||
if (count <= 1) {
|
||||
animatedImage = [[UIImage alloc] initWithData:data];
|
||||
}
|
||||
else {
|
||||
NSMutableArray *images = [NSMutableArray array];
|
||||
|
||||
NSTimeInterval duration = 0.0f;
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
|
||||
if (!image) {
|
||||
continue;
|
||||
}
|
||||
|
||||
duration += [self sd_frameDurationAtIndex:i source:source];
|
||||
|
||||
[images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
|
||||
|
||||
CGImageRelease(image);
|
||||
}
|
||||
|
||||
if (!duration) {
|
||||
duration = (1.0f / 10.0f) * count;
|
||||
}
|
||||
|
||||
animatedImage = [UIImage animatedImageWithImages:images duration:duration];
|
||||
staticImage = [[UIImage alloc] initWithData:data];
|
||||
} else {
|
||||
// we will only retrieve the 1st frame. the full GIF support is available via the FLAnimatedImageView category.
|
||||
// this here is only code to allow drawing animated images as static ones
|
||||
CGImageRef CGImage = CGImageSourceCreateImageAtIndex(source, 0, NULL);
|
||||
UIImage *frameImage = [UIImage imageWithCGImage:CGImage scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
|
||||
staticImage = [UIImage animatedImageWithImages:@[frameImage] duration:0.0f];
|
||||
CGImageRelease(CGImage);
|
||||
}
|
||||
|
||||
CFRelease(source);
|
||||
|
||||
return animatedImage;
|
||||
return staticImage;
|
||||
}
|
||||
|
||||
+ (float)sd_frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source {
|
||||
float frameDuration = 0.1f;
|
||||
CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil);
|
||||
NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties;
|
||||
NSDictionary *gifProperties = frameProperties[(NSString *)kCGImagePropertyGIFDictionary];
|
||||
|
||||
NSNumber *delayTimeUnclampedProp = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
|
||||
if (delayTimeUnclampedProp) {
|
||||
frameDuration = delayTimeUnclampedProp.floatValue;
|
||||
}
|
||||
else {
|
||||
|
||||
NSNumber *delayTimeProp = gifProperties[(NSString *)kCGImagePropertyGIFDelayTime];
|
||||
if (delayTimeProp) {
|
||||
frameDuration = delayTimeProp.floatValue;
|
||||
}
|
||||
}
|
||||
|
||||
// Many annoying ads specify a 0 duration to make an image flash as quickly as possible.
|
||||
// We follow Firefox's behavior and use a duration of 100 ms for any frames that specify
|
||||
// a duration of <= 10 ms. See <rdar://problem/7689300> and <http://webkit.org/b/36082>
|
||||
// for more information.
|
||||
|
||||
if (frameDuration < 0.011f) {
|
||||
frameDuration = 0.100f;
|
||||
}
|
||||
|
||||
CFRelease(cfFrameProperties);
|
||||
return frameDuration;
|
||||
}
|
||||
|
||||
+ (UIImage *)sd_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 sd_animatedGIFWithData:data];
|
||||
}
|
||||
|
||||
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
|
||||
|
||||
data = [NSData dataWithContentsOfFile:path];
|
||||
|
||||
if (data) {
|
||||
return [UIImage sd_animatedGIFWithData:data];
|
||||
}
|
||||
|
||||
return [UIImage imageNamed:name];
|
||||
}
|
||||
else {
|
||||
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
|
||||
|
||||
NSData *data = [NSData dataWithContentsOfFile:path];
|
||||
|
||||
if (data) {
|
||||
return [UIImage sd_animatedGIFWithData:data];
|
||||
}
|
||||
|
||||
return [UIImage imageNamed:name];
|
||||
}
|
||||
}
|
||||
|
||||
- (UIImage *)sd_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];
|
||||
|
||||
for (UIImage *image in self.images) {
|
||||
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
|
||||
|
||||
[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];
|
||||
- (BOOL)isGIF {
|
||||
return (self.images != nil);
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
Loading…
Reference in New Issue