UIImage+MultiFormat.m 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 "objc/runtime.h"
  10. #import "SDWebImageCodersManager.h"
  11. @implementation UIImage (MultiFormat)
  12. - (NSUInteger)sd_imageLoopCount {
  13. NSUInteger imageLoopCount = 0;
  14. NSNumber *value = objc_getAssociatedObject(self, @selector(sd_imageLoopCount));
  15. if ([value isKindOfClass:[NSNumber class]]) {
  16. imageLoopCount = value.unsignedIntegerValue;
  17. }
  18. return imageLoopCount;
  19. }
  20. - (void)setSd_imageLoopCount:(NSUInteger)sd_imageLoopCount {
  21. NSNumber *value = @(sd_imageLoopCount);
  22. objc_setAssociatedObject(self, @selector(sd_imageLoopCount), value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  23. }
  24. + (nullable UIImage *)sd_imageWithData:(nullable NSData *)data {
  25. return [[SDWebImageCodersManager sharedInstance] decodedImageWithData:data];
  26. }
  27. - (nullable NSData *)sd_imageData {
  28. return [self sd_imageDataAsFormat:SDImageFormatUndefined];
  29. }
  30. - (nullable NSData *)sd_imageDataAsFormat:(SDImageFormat)imageFormat {
  31. NSData *imageData = nil;
  32. if (self) {
  33. imageData = [[SDWebImageCodersManager sharedInstance] encodedDataWithImage:self format:imageFormat];
  34. }
  35. return imageData;
  36. }
  37. @end