UIImage+MultiFormat.m 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import "UIImage+MultiFormat.h"
  9. #import "NSImage+WebCache.h"
  10. #import "SDWebImageCodersManager.h"
  11. #import "objc/runtime.h"
  12. @implementation UIImage (MultiFormat)
  13. #if SD_MAC
  14. - (NSUInteger)sd_imageLoopCount {
  15. NSUInteger imageLoopCount = 0;
  16. for (NSImageRep *rep in self.representations) {
  17. if ([rep isKindOfClass:[NSBitmapImageRep class]]) {
  18. NSBitmapImageRep *bitmapRep = (NSBitmapImageRep *)rep;
  19. imageLoopCount = [[bitmapRep valueForProperty:NSImageLoopCount] unsignedIntegerValue];
  20. break;
  21. }
  22. }
  23. return imageLoopCount;
  24. }
  25. - (void)setSd_imageLoopCount:(NSUInteger)sd_imageLoopCount {
  26. for (NSImageRep *rep in self.representations) {
  27. if ([rep isKindOfClass:[NSBitmapImageRep class]]) {
  28. NSBitmapImageRep *bitmapRep = (NSBitmapImageRep *)rep;
  29. [bitmapRep setProperty:NSImageLoopCount withValue:@(sd_imageLoopCount)];
  30. break;
  31. }
  32. }
  33. }
  34. #else
  35. - (NSUInteger)sd_imageLoopCount {
  36. NSUInteger imageLoopCount = 0;
  37. NSNumber *value = objc_getAssociatedObject(self, @selector(sd_imageLoopCount));
  38. if ([value isKindOfClass:[NSNumber class]]) {
  39. imageLoopCount = value.unsignedIntegerValue;
  40. }
  41. return imageLoopCount;
  42. }
  43. - (void)setSd_imageLoopCount:(NSUInteger)sd_imageLoopCount {
  44. NSNumber *value = @(sd_imageLoopCount);
  45. objc_setAssociatedObject(self, @selector(sd_imageLoopCount), value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  46. }
  47. #endif
  48. - (SDImageFormat)sd_imageFormat {
  49. SDImageFormat imageFormat = SDImageFormatUndefined;
  50. NSNumber *value = objc_getAssociatedObject(self, @selector(sd_imageFormat));
  51. if ([value isKindOfClass:[NSNumber class]]) {
  52. imageFormat = value.integerValue;
  53. return imageFormat;
  54. }
  55. // Check CGImage's UTType, may return nil for non-Image/IO based image
  56. #pragma clang diagnostic push
  57. #pragma clang diagnostic ignored "-Wunguarded-availability"
  58. if (&CGImageGetUTType != NULL) {
  59. CFStringRef uttype = CGImageGetUTType(self.CGImage);
  60. imageFormat = [NSData sd_imageFormatFromUTType:uttype];
  61. }
  62. #pragma clang diagnostic pop
  63. return imageFormat;
  64. }
  65. - (void)setSd_imageFormat:(SDImageFormat)sd_imageFormat {
  66. objc_setAssociatedObject(self, @selector(sd_imageFormat), @(sd_imageFormat), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  67. }
  68. + (nullable UIImage *)sd_imageWithData:(nullable NSData *)data {
  69. return [[SDWebImageCodersManager sharedInstance] decodedImageWithData:data];
  70. }
  71. - (nullable NSData *)sd_imageData {
  72. return [self sd_imageDataAsFormat:SDImageFormatUndefined];
  73. }
  74. - (nullable NSData *)sd_imageDataAsFormat:(SDImageFormat)imageFormat {
  75. NSData *imageData = nil;
  76. if (self) {
  77. imageData = [[SDWebImageCodersManager sharedInstance] encodedDataWithImage:self format:imageFormat];
  78. }
  79. return imageData;
  80. }
  81. @end