UIImage+GIF.m 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
  19. size_t count = CGImageSourceGetCount(source);
  20. UIImage *staticImage;
  21. if (count <= 1) {
  22. staticImage = [[UIImage alloc] initWithData:data];
  23. } else {
  24. // we will only retrieve the 1st frame. the full GIF support is available via the FLAnimatedImageView category.
  25. // this here is only code to allow drawing animated images as static ones
  26. #if SD_WATCH
  27. CGFloat scale = 1;
  28. scale = [WKInterfaceDevice currentDevice].screenScale;
  29. #elif SD_UIKIT
  30. CGFloat scale = 1;
  31. scale = [UIScreen mainScreen].scale;
  32. #endif
  33. CGImageRef CGImage = CGImageSourceCreateImageAtIndex(source, 0, NULL);
  34. #if SD_UIKIT || SD_WATCH
  35. UIImage *frameImage = [UIImage imageWithCGImage:CGImage scale:scale orientation:UIImageOrientationUp];
  36. staticImage = [UIImage animatedImageWithImages:@[frameImage] duration:0.0f];
  37. #elif SD_MAC
  38. staticImage = [[UIImage alloc] initWithCGImage:CGImage size:NSZeroSize];
  39. #endif
  40. CGImageRelease(CGImage);
  41. }
  42. CFRelease(source);
  43. return staticImage;
  44. }
  45. - (BOOL)isGIF {
  46. return (self.images != nil);
  47. }
  48. @end