SDWebImageCompat.m 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 "SDWebImageCompat.h"
  9. #import "objc/runtime.h"
  10. #if !__has_feature(objc_arc)
  11. #error SDWebImage is ARC only. Either turn on ARC for the project or use -fobjc-arc flag
  12. #endif
  13. inline UIImage *SDScaledImageForKey(NSString * _Nullable key, UIImage * _Nullable image) {
  14. if (!image) {
  15. return nil;
  16. }
  17. #if SD_MAC
  18. return image;
  19. #elif SD_UIKIT || SD_WATCH
  20. if ((image.images).count > 0) {
  21. NSMutableArray<UIImage *> *scaledImages = [NSMutableArray array];
  22. for (UIImage *tempImage in image.images) {
  23. [scaledImages addObject:SDScaledImageForKey(key, tempImage)];
  24. }
  25. UIImage *animatedImage = [UIImage animatedImageWithImages:scaledImages duration:image.duration];
  26. #ifdef SD_WEBP
  27. if (animatedImage) {
  28. SEL sd_webpLoopCount = NSSelectorFromString(@"sd_webpLoopCount");
  29. NSNumber *value = objc_getAssociatedObject(image, sd_webpLoopCount);
  30. NSInteger loopCount = value.integerValue;
  31. if (loopCount) {
  32. objc_setAssociatedObject(animatedImage, sd_webpLoopCount, @(loopCount), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  33. }
  34. }
  35. #endif
  36. return animatedImage;
  37. } else {
  38. #if SD_WATCH
  39. if ([[WKInterfaceDevice currentDevice] respondsToSelector:@selector(screenScale)]) {
  40. #elif SD_UIKIT
  41. if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
  42. #endif
  43. CGFloat scale = 1;
  44. if (key.length >= 8) {
  45. NSRange range = [key rangeOfString:@"@2x."];
  46. if (range.location != NSNotFound) {
  47. scale = 2.0;
  48. }
  49. range = [key rangeOfString:@"@3x."];
  50. if (range.location != NSNotFound) {
  51. scale = 3.0;
  52. }
  53. }
  54. UIImage *scaledImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:image.imageOrientation];
  55. image = scaledImage;
  56. }
  57. return image;
  58. }
  59. #endif
  60. }
  61. NSString *const SDWebImageErrorDomain = @"SDWebImageErrorDomain";