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 "UIImage+MultiFormat.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. #if !OS_OBJECT_USE_OBJC
  14. #error SDWebImage need ARC for dispatch object
  15. #endif
  16. inline UIImage *SDScaledImageForKey(NSString * _Nullable key, UIImage * _Nullable image) {
  17. if (!image) {
  18. return nil;
  19. }
  20. #if SD_MAC
  21. return image;
  22. #elif SD_UIKIT || SD_WATCH
  23. if ((image.images).count > 0) {
  24. NSMutableArray<UIImage *> *scaledImages = [NSMutableArray array];
  25. for (UIImage *tempImage in image.images) {
  26. [scaledImages addObject:SDScaledImageForKey(key, tempImage)];
  27. }
  28. UIImage *animatedImage = [UIImage animatedImageWithImages:scaledImages duration:image.duration];
  29. if (animatedImage) {
  30. animatedImage.sd_imageLoopCount = image.sd_imageLoopCount;
  31. animatedImage.sd_imageFormat = image.sd_imageFormat;
  32. }
  33. return animatedImage;
  34. } else {
  35. #if SD_WATCH
  36. if ([[WKInterfaceDevice currentDevice] respondsToSelector:@selector(screenScale)]) {
  37. #elif SD_UIKIT
  38. if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
  39. #endif
  40. CGFloat scale = 1;
  41. if (key.length >= 8) {
  42. NSRange range = [key rangeOfString:@"@2x."];
  43. if (range.location != NSNotFound) {
  44. scale = 2.0;
  45. }
  46. range = [key rangeOfString:@"@3x."];
  47. if (range.location != NSNotFound) {
  48. scale = 3.0;
  49. }
  50. }
  51. if (scale != image.scale) {
  52. UIImage *scaledImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:image.imageOrientation];
  53. scaledImage.sd_imageFormat = image.sd_imageFormat;
  54. image = scaledImage;
  55. }
  56. }
  57. return image;
  58. }
  59. #endif
  60. }
  61. NSString *const SDWebImageErrorDomain = @"SDWebImageErrorDomain";