SDWebImageCompat.m 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //
  2. // SDWebImageCompat.m
  3. // SDWebImage
  4. //
  5. // Created by Olivier Poitrey on 11/12/12.
  6. // Copyright (c) 2012 Dailymotion. All rights reserved.
  7. //
  8. #import "SDWebImageCompat.h"
  9. #if !__has_feature(objc_arc)
  10. #error SDWebImage is ARC only. Either turn on ARC for the project or use -fobjc-arc flag
  11. #endif
  12. inline UIImage *SDScaledImageForKey(NSString *key, UIImage *image) {
  13. if (!image) {
  14. return nil;
  15. }
  16. if ([image.images count] > 0) {
  17. NSMutableArray *scaledImages = [NSMutableArray array];
  18. for (UIImage *tempImage in image.images) {
  19. [scaledImages addObject:SDScaledImageForKey(key, tempImage)];
  20. }
  21. return [UIImage animatedImageWithImages:scaledImages duration:image.duration];
  22. }
  23. else {
  24. if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
  25. CGFloat scale = 1.0;
  26. if (key.length >= 8) {
  27. // Search @2x. at the end of the string, before a 3 to 4 extension length (only if key len is 8 or more @2x. + 4 len ext)
  28. NSRange range = [key rangeOfString:@"@2x." options:0 range:NSMakeRange(key.length - 8, 5)];
  29. if (range.location != NSNotFound) {
  30. scale = 2.0;
  31. }
  32. }
  33. UIImage *scaledImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:image.imageOrientation];
  34. image = scaledImage;
  35. }
  36. return image;
  37. }
  38. }