134 lines
3.7 KiB
Objective-C
Executable File
134 lines
3.7 KiB
Objective-C
Executable File
//
|
|
// 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
|