SNKNinePatchImageCache.m 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // SNKNinePatchImageCache.m
  3. // TestImageResible
  4. //
  5. // Created by tu jinqiu on 2018/11/7.
  6. // Copyright © 2018年 tu jinqiu. All rights reserved.
  7. //
  8. #import "SNKNinePatchImageCache.h"
  9. @interface SNKNinePatchImageCache ()
  10. @property(nonatomic, strong) NSCache *imagesCache;
  11. @end
  12. @implementation SNKNinePatchImageCache
  13. + (instancetype)sharedCache
  14. {
  15. static SNKNinePatchImageCache *instance = nil;
  16. static dispatch_once_t onceToken;
  17. dispatch_once(&onceToken, ^{
  18. instance = [SNKNinePatchImageCache new];
  19. instance.imagesCache = [NSCache new];
  20. });
  21. return instance;
  22. }
  23. + (void)setNinePatchImage:(SNKNinePatchImage *)ninePatchImage forName:(NSString *)name
  24. {
  25. [[SNKNinePatchImageCache sharedCache] setNinePatchImage:ninePatchImage forName:name];
  26. }
  27. + (SNKNinePatchImage *)ninePatchImageNamed:(NSString *)name
  28. {
  29. SNKNinePatchImage *patchImage = [[SNKNinePatchImageCache sharedCache] ninePatchImageNamed:name];
  30. if (patchImage == nil) {
  31. [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[LCTools getImageUrlWithAddress:name] options:SDWebImageDownloaderUseNSURLCache progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
  32. [SNKNinePatchImageCache setNinePatchImage:[SNKNinePatchImage ninePatchImageWithImageData:data scale:2] forName:[LCTools getImageUrlWithAddress:name].absoluteString];
  33. }];
  34. }
  35. return patchImage;
  36. }
  37. - (void)setNinePatchImage:(SNKNinePatchImage *)ninePatchImage forName:(NSString *)name
  38. {
  39. if (ninePatchImage == nil) {
  40. return;
  41. }
  42. [self.imagesCache setObject:ninePatchImage forKey:name];
  43. }
  44. - (SNKNinePatchImage *)ninePatchImageNamed:(NSString *)name
  45. {
  46. return [self.imagesCache objectForKey:name];
  47. }
  48. @end