UIImage+MemoryCacheCost.m 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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+MemoryCacheCost.h"
  9. #import "objc/runtime.h"
  10. FOUNDATION_STATIC_INLINE NSUInteger SDMemoryCacheCostForImage(UIImage *image) {
  11. #if SD_MAC
  12. return image.size.height * image.size.width;
  13. #elif SD_UIKIT || SD_WATCH
  14. NSUInteger imageSize = image.size.height * image.size.width * image.scale * image.scale;
  15. return image.images ? (imageSize * image.images.count) : imageSize;
  16. #endif
  17. }
  18. @implementation UIImage (MemoryCacheCost)
  19. - (NSUInteger)sd_memoryCost {
  20. NSNumber *value = objc_getAssociatedObject(self, @selector(sd_memoryCost));
  21. NSUInteger memoryCost;
  22. if (value != nil) {
  23. memoryCost = [value unsignedIntegerValue];
  24. } else {
  25. memoryCost = SDMemoryCacheCostForImage(self);
  26. }
  27. return memoryCost;
  28. }
  29. - (void)setSd_memoryCost:(NSUInteger)sd_memoryCost {
  30. objc_setAssociatedObject(self, @selector(sd_memoryCost), @(sd_memoryCost), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  31. }
  32. @end