UIImage+GIF.m 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. * (c) Laurin Brandner
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. #import "UIImage+GIF.h"
  10. #import <ImageIO/ImageIO.h>
  11. #import "objc/runtime.h"
  12. #import "NSImage+WebCache.h"
  13. @implementation UIImage (GIF)
  14. + (UIImage *)sd_animatedGIFWithData:(NSData *)data {
  15. if (!data) {
  16. return nil;
  17. }
  18. #if SD_MAC
  19. return [[UIImage alloc] initWithData:data];
  20. #else
  21. CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
  22. size_t count = CGImageSourceGetCount(source);
  23. UIImage *staticImage;
  24. if (count <= 1) {
  25. staticImage = [[UIImage alloc] initWithData:data];
  26. } else {
  27. // we will only retrieve the 1st frame. the full GIF support is available via the FLAnimatedImageView category.
  28. // this here is only code to allow drawing animated images as static ones
  29. #if SD_WATCH
  30. CGFloat scale = 1;
  31. scale = [WKInterfaceDevice currentDevice].screenScale;
  32. #elif SD_UIKIT
  33. CGFloat scale = 1;
  34. scale = [UIScreen mainScreen].scale;
  35. #endif
  36. CGImageRef CGImage = CGImageSourceCreateImageAtIndex(source, 0, NULL);
  37. #if SD_UIKIT || SD_WATCH
  38. UIImage *frameImage = [UIImage imageWithCGImage:CGImage scale:scale orientation:UIImageOrientationUp];
  39. staticImage = [UIImage animatedImageWithImages:@[frameImage] duration:0.0f];
  40. #endif
  41. CGImageRelease(CGImage);
  42. }
  43. CFRelease(source);
  44. return staticImage;
  45. #endif
  46. }
  47. - (BOOL)isGIF {
  48. return (self.images != nil);
  49. }
  50. @end