UIImage+GIF.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // UIImage+GIF.m
  3. // LBGIFImage
  4. //
  5. // Created by Laurin Brandner on 06.01.12.
  6. // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "UIImage+GIF.h"
  9. #import <ImageIO/ImageIO.h>
  10. @implementation UIImage (GIF)
  11. + (UIImage *)sd_animatedGIFWithData:(NSData *)data {
  12. if (!data) {
  13. return nil;
  14. }
  15. CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
  16. size_t count = CGImageSourceGetCount(source);
  17. UIImage *animatedImage;
  18. if (count <= 1) {
  19. animatedImage = [[UIImage alloc] initWithData:data];
  20. }
  21. else {
  22. NSMutableArray *images = [NSMutableArray array];
  23. NSTimeInterval duration = 0.0f;
  24. for (size_t i = 0; i < count; i++) {
  25. CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
  26. duration += [self sd_frameDurationAtIndex:i source:source];
  27. [images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
  28. CGImageRelease(image);
  29. }
  30. if (!duration) {
  31. duration = (1.0f / 10.0f) * count;
  32. }
  33. animatedImage = [UIImage animatedImageWithImages:images duration:duration];
  34. }
  35. CFRelease(source);
  36. return animatedImage;
  37. }
  38. + (float)sd_frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source {
  39. float frameDuration = 0.1f;
  40. CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil);
  41. NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties;
  42. NSDictionary *gifProperties = frameProperties[(NSString *)kCGImagePropertyGIFDictionary];
  43. NSNumber *delayTimeUnclampedProp = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
  44. if (delayTimeUnclampedProp) {
  45. frameDuration = [delayTimeUnclampedProp floatValue];
  46. }
  47. else {
  48. NSNumber *delayTimeProp = gifProperties[(NSString *)kCGImagePropertyGIFDelayTime];
  49. if (delayTimeProp) {
  50. frameDuration = [delayTimeProp floatValue];
  51. }
  52. }
  53. // Many annoying ads specify a 0 duration to make an image flash as quickly as possible.
  54. // We follow Firefox's behavior and use a duration of 100 ms for any frames that specify
  55. // a duration of <= 10 ms. See <rdar://problem/7689300> and <http://webkit.org/b/36082>
  56. // for more information.
  57. if (frameDuration < 0.011f) {
  58. frameDuration = 0.100f;
  59. }
  60. CFRelease(cfFrameProperties);
  61. return frameDuration;
  62. }
  63. + (UIImage *)sd_animatedGIFNamed:(NSString *)name {
  64. CGFloat scale = [UIScreen mainScreen].scale;
  65. if (scale > 1.0f) {
  66. NSString *retinaPath = [[NSBundle mainBundle] pathForResource:[name stringByAppendingString:@"@2x"] ofType:@"gif"];
  67. NSData *data = [NSData dataWithContentsOfFile:retinaPath];
  68. if (data) {
  69. return [UIImage sd_animatedGIFWithData:data];
  70. }
  71. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
  72. data = [NSData dataWithContentsOfFile:path];
  73. if (data) {
  74. return [UIImage sd_animatedGIFWithData:data];
  75. }
  76. return [UIImage imageNamed:name];
  77. }
  78. else {
  79. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
  80. NSData *data = [NSData dataWithContentsOfFile:path];
  81. if (data) {
  82. return [UIImage sd_animatedGIFWithData:data];
  83. }
  84. return [UIImage imageNamed:name];
  85. }
  86. }
  87. - (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size {
  88. if (CGSizeEqualToSize(self.size, size) || CGSizeEqualToSize(size, CGSizeZero)) {
  89. return self;
  90. }
  91. CGSize scaledSize = size;
  92. CGPoint thumbnailPoint = CGPointZero;
  93. CGFloat widthFactor = size.width / self.size.width;
  94. CGFloat heightFactor = size.height / self.size.height;
  95. CGFloat scaleFactor = (widthFactor > heightFactor) ? widthFactor : heightFactor;
  96. scaledSize.width = self.size.width * scaleFactor;
  97. scaledSize.height = self.size.height * scaleFactor;
  98. if (widthFactor > heightFactor) {
  99. thumbnailPoint.y = (size.height - scaledSize.height) * 0.5;
  100. }
  101. else if (widthFactor < heightFactor) {
  102. thumbnailPoint.x = (size.width - scaledSize.width) * 0.5;
  103. }
  104. NSMutableArray *scaledImages = [NSMutableArray array];
  105. UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
  106. for (UIImage *image in self.images) {
  107. [image drawInRect:CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledSize.width, scaledSize.height)];
  108. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  109. [scaledImages addObject:newImage];
  110. }
  111. UIGraphicsEndImageContext();
  112. return [UIImage animatedImageWithImages:scaledImages duration:self.duration];
  113. }
  114. @end