UIImageView+ZFCache.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. //
  2. // UIImageView+ZFCache.m
  3. // ZFPlayer
  4. //
  5. // Copyright (c) 2016年 任子丰 ( http://github.com/renzifeng )
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #import "UIImageView+ZFCache.h"
  25. #import <objc/runtime.h>
  26. #import <CommonCrypto/CommonDigest.h>
  27. @implementation ZFImageDownloader
  28. - (void)startDownloadImageWithUrl:(NSString *)url
  29. progress:(ZFDownloadProgressBlock)progress
  30. finished:(ZFDownLoadDataCallBack)finished {
  31. self.progressBlock = progress;
  32. self.callbackOnFinished = finished;
  33. if ([NSURL URLWithString:url] == nil) {
  34. if (finished) { finished(nil, nil); }
  35. return;
  36. }
  37. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
  38. cachePolicy:NSURLRequestReturnCacheDataElseLoad
  39. timeoutInterval:60];
  40. [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
  41. NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
  42. NSOperationQueue *queue = [[NSOperationQueue alloc]init];
  43. self.session = [NSURLSession sessionWithConfiguration:config
  44. delegate:self
  45. delegateQueue:queue];
  46. NSURLSessionDownloadTask *task = [self.session downloadTaskWithRequest:request];
  47. [task resume];
  48. self.task = task;
  49. }
  50. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
  51. NSData *data = [NSData dataWithContentsOfURL:location];
  52. if (self.progressBlock) {
  53. self.progressBlock(self.totalLength, self.currentLength);
  54. }
  55. if (self.callbackOnFinished) {
  56. self.callbackOnFinished(data, nil);
  57. // 防止重复调用
  58. self.callbackOnFinished = nil;
  59. }
  60. }
  61. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
  62. self.currentLength = totalBytesWritten;
  63. self.totalLength = totalBytesExpectedToWrite;
  64. if (self.progressBlock) {
  65. self.progressBlock(self.totalLength, self.currentLength);
  66. }
  67. }
  68. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
  69. if ([error code] != NSURLErrorCancelled) {
  70. if (self.callbackOnFinished) {
  71. self.callbackOnFinished(nil, error);
  72. }
  73. self.callbackOnFinished = nil;
  74. }
  75. }
  76. @end
  77. @interface NSString (md5)
  78. + (NSString *)cachedFileNameForKey:(NSString *)key;
  79. + (NSString *)zf_cachePath;
  80. + (NSString *)zf_keyForRequest:(NSURLRequest *)request;
  81. @end
  82. @implementation NSString (md5)
  83. + (NSString *)zf_keyForRequest:(NSURLRequest *)request {
  84. return request.URL.absoluteString;
  85. }
  86. + (NSString *)zf_cachePath {
  87. NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
  88. NSString *directoryPath = [NSString stringWithFormat:@"%@/%@/%@",cachePath,@"default",@"com.hackemist.SDWebImageCache.default"];
  89. return directoryPath;
  90. }
  91. + (NSString *)cachedFileNameForKey:(NSString *)key {
  92. const char *str = [key UTF8String];
  93. if (str == NULL) {
  94. str = "";
  95. }
  96. unsigned char r[CC_MD5_DIGEST_LENGTH];
  97. CC_MD5(str, (CC_LONG)strlen(str), r);
  98. NSString *filename = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%@",
  99. r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10],
  100. r[11], r[12], r[13], r[14], r[15], [[key pathExtension] isEqualToString:@""] ? @"" : [NSString stringWithFormat:@".%@", [key pathExtension]]];
  101. return filename;
  102. }
  103. @end
  104. @interface UIApplication (ZFCacheImage)
  105. @property (nonatomic, strong, readonly) NSMutableDictionary *zf_cacheFaileTimes;
  106. - (UIImage *)zf_cacheImageForRequest:(NSURLRequest *)request;
  107. - (void)zf_cacheImage:(UIImage *)image forRequest:(NSURLRequest *)request;
  108. - (void)zf_cacheFailRequest:(NSURLRequest *)request;
  109. - (NSUInteger)zf_failTimesForRequest:(NSURLRequest *)request;
  110. @end
  111. @implementation UIApplication (ZFCacheImage)
  112. - (NSMutableDictionary *)zf_cacheFaileTimes {
  113. NSMutableDictionary *dict = objc_getAssociatedObject(self, _cmd);
  114. if (!dict) {
  115. dict = [[NSMutableDictionary alloc] init];
  116. }
  117. return dict;
  118. }
  119. - (void)setZf_cacheFaileTimes:(NSMutableDictionary *)zf_cacheFaileTimes {
  120. objc_setAssociatedObject(self, @selector(zf_cacheFaileTimes), zf_cacheFaileTimes, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  121. }
  122. - (void)zf_clearCache {
  123. [self.zf_cacheFaileTimes removeAllObjects];
  124. self.zf_cacheFaileTimes = nil;
  125. }
  126. - (void)zf_clearDiskCaches {
  127. NSString *directoryPath = [NSString zf_cachePath];
  128. if ([[NSFileManager defaultManager] fileExistsAtPath:directoryPath isDirectory:nil]) {
  129. dispatch_queue_t ioQueue = dispatch_queue_create("com.hackemist.SDWebImageCache", DISPATCH_QUEUE_SERIAL);
  130. dispatch_async(ioQueue, ^{
  131. [[NSFileManager defaultManager] removeItemAtPath:directoryPath error:nil];
  132. [[NSFileManager defaultManager] createDirectoryAtPath:directoryPath
  133. withIntermediateDirectories:YES
  134. attributes:nil
  135. error:nil];
  136. });
  137. }
  138. [self zf_clearCache];
  139. }
  140. - (UIImage *)zf_cacheImageForRequest:(NSURLRequest *)request {
  141. if (request) {
  142. NSString *directoryPath = [NSString zf_cachePath];
  143. NSString *path = [NSString stringWithFormat:@"%@/%@", directoryPath, [NSString cachedFileNameForKey:[NSString zf_keyForRequest:request]]];
  144. return [UIImage imageWithContentsOfFile:path];
  145. }
  146. return nil;
  147. }
  148. - (NSUInteger)zf_failTimesForRequest:(NSURLRequest *)request {
  149. NSNumber *faileTimes = [self.zf_cacheFaileTimes objectForKey:[NSString cachedFileNameForKey:[NSString zf_keyForRequest:request]]];
  150. if (faileTimes && [faileTimes respondsToSelector:@selector(integerValue)]) {
  151. return faileTimes.integerValue;
  152. }
  153. return 0;
  154. }
  155. - (void)zf_cacheFailRequest:(NSURLRequest *)request {
  156. NSNumber *faileTimes = [self.zf_cacheFaileTimes objectForKey:[NSString cachedFileNameForKey:[NSString zf_keyForRequest:request]]];
  157. NSUInteger times = 0;
  158. if (faileTimes && [faileTimes respondsToSelector:@selector(integerValue)]) {
  159. times = [faileTimes integerValue];
  160. }
  161. times++;
  162. [self.zf_cacheFaileTimes setObject:@(times) forKey:[NSString cachedFileNameForKey:[NSString zf_keyForRequest:request]]];
  163. }
  164. - (void)zf_cacheImage:(UIImage *)image forRequest:(NSURLRequest *)request {
  165. if (!image || !request) { return; }
  166. NSString *directoryPath = [NSString zf_cachePath];
  167. if (![[NSFileManager defaultManager] fileExistsAtPath:directoryPath isDirectory:nil]) {
  168. NSError *error = nil;
  169. [[NSFileManager defaultManager] createDirectoryAtPath:directoryPath
  170. withIntermediateDirectories:YES
  171. attributes:nil
  172. error:&error];
  173. if (error) { return; }
  174. }
  175. NSString *path = [NSString stringWithFormat:@"%@/%@", directoryPath, [NSString cachedFileNameForKey:[NSString zf_keyForRequest:request]]];
  176. NSData *data = UIImagePNGRepresentation(image);
  177. if (data) {
  178. [[NSFileManager defaultManager] createFileAtPath:path contents:data attributes:nil];
  179. }
  180. }
  181. @end
  182. @implementation UIImageView (ZFCache)
  183. #pragma mark - getter
  184. - (ZFImageBlock)completion {
  185. return objc_getAssociatedObject(self, _cmd);
  186. }
  187. - (ZFImageDownloader *)imageDownloader {
  188. return objc_getAssociatedObject(self, _cmd);
  189. }
  190. - (NSUInteger)attemptToReloadTimesForFailedURL {
  191. NSUInteger count = [objc_getAssociatedObject(self, _cmd) integerValue];
  192. if (count == 0) { count = 2; }
  193. return count;
  194. }
  195. - (BOOL)shouldAutoClipImageToViewSize {
  196. return [objc_getAssociatedObject(self, _cmd) boolValue];
  197. }
  198. #pragma mark - setter
  199. - (void)setCompletion:(ZFImageBlock)completion {
  200. objc_setAssociatedObject(self, @selector(completion), completion, OBJC_ASSOCIATION_COPY_NONATOMIC);
  201. }
  202. - (void)setImageDownloader:(ZFImageDownloader *)imageDownloader {
  203. objc_setAssociatedObject(self, @selector(imageDownloader), imageDownloader, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  204. }
  205. - (void)setAttemptToReloadTimesForFailedURL:(NSUInteger)attemptToReloadTimesForFailedURL {
  206. objc_setAssociatedObject(self, @selector(attemptToReloadTimesForFailedURL), @(attemptToReloadTimesForFailedURL), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  207. }
  208. - (void)setShouldAutoClipImageToViewSize:(BOOL)shouldAutoClipImageToViewSize {
  209. objc_setAssociatedObject(self, @selector(shouldAutoClipImageToViewSize), @(shouldAutoClipImageToViewSize), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  210. }
  211. #pragma mark - public method
  212. - (void)setImageWithURLString:(NSString *)url
  213. placeholderImageName:(NSString *)placeholderImageName {
  214. return [self setImageWithURLString:url placeholderImageName:placeholderImageName completion:nil];
  215. }
  216. - (void)setImageWithURLString:(NSString *)url placeholder:(UIImage *)placeholderImage {
  217. return [self setImageWithURLString:url placeholder:placeholderImage completion:nil];
  218. }
  219. - (void)setImageWithURLString:(NSString *)url
  220. placeholderImageName:(NSString *)placeholderImage
  221. completion:(void (^)(UIImage *image))completion {
  222. NSString *path = [[NSBundle mainBundle] pathForResource:placeholderImage ofType:nil];
  223. UIImage *image = [UIImage imageWithContentsOfFile:path];
  224. if (image == nil) { image = [UIImage imageNamed:placeholderImage]; }
  225. [self setImageWithURLString:url placeholder:image completion:completion];
  226. }
  227. - (void)setImageWithURLString:(NSString *)url
  228. placeholder:(UIImage *)placeholderImageName
  229. completion:(void (^)(UIImage *image))completion {
  230. [self.layer removeAllAnimations];
  231. self.completion = completion;
  232. if (url == nil || [url isKindOfClass:[NSNull class]] || (![url hasPrefix:@"http://"] && ![url hasPrefix:@"https://"])) {
  233. [self setImage:placeholderImageName isFromCache:YES];
  234. if (completion) {
  235. self.completion(self.image);
  236. }
  237. return;
  238. }
  239. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
  240. [self downloadWithReqeust:request holder:placeholderImageName];
  241. }
  242. #pragma mark - private method
  243. - (void)downloadWithReqeust:(NSURLRequest *)theRequest holder:(UIImage *)holder {
  244. UIImage *cachedImage = [[UIApplication sharedApplication] zf_cacheImageForRequest:theRequest];
  245. if (cachedImage) {
  246. [self setImage:cachedImage isFromCache:YES];
  247. if (self.completion) {
  248. self.completion(cachedImage);
  249. }
  250. return;
  251. }
  252. [self setImage:holder isFromCache:YES];
  253. if ([[UIApplication sharedApplication] zf_failTimesForRequest:theRequest] >= self.attemptToReloadTimesForFailedURL) {
  254. return;
  255. }
  256. [self cancelRequest];
  257. self.imageDownloader = nil;
  258. __weak __typeof(self) weakSelf = self;
  259. self.imageDownloader = [[ZFImageDownloader alloc] init];
  260. [self.imageDownloader startDownloadImageWithUrl:theRequest.URL.absoluteString progress:nil finished:^(NSData *data, NSError *error) {
  261. // success
  262. if (data != nil && error == nil) {
  263. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  264. UIImage *image = [UIImage imageWithData:data];
  265. UIImage *finalImage = image;
  266. if (image) {
  267. if (weakSelf.shouldAutoClipImageToViewSize) {
  268. // cutting
  269. if (fabs(weakSelf.frame.size.width - image.size.width) != 0
  270. && fabs(weakSelf.frame.size.height - image.size.height) != 0) {
  271. finalImage = [self clipImage:image toSize:weakSelf.frame.size isScaleToMax:YES];
  272. }
  273. }
  274. [[UIApplication sharedApplication] zf_cacheImage:finalImage forRequest:theRequest];
  275. } else {
  276. [[UIApplication sharedApplication] zf_cacheFailRequest:theRequest];
  277. }
  278. dispatch_async(dispatch_get_main_queue(), ^{
  279. if (finalImage) {
  280. [weakSelf setImage:finalImage isFromCache:NO];
  281. if (weakSelf.completion) {
  282. weakSelf.completion(weakSelf.image);
  283. }
  284. } else {// error data
  285. if (weakSelf.completion) {
  286. weakSelf.completion(weakSelf.image);
  287. }
  288. }
  289. });
  290. });
  291. } else { // error
  292. [[UIApplication sharedApplication] zf_cacheFailRequest:theRequest];
  293. if (weakSelf.completion) {
  294. weakSelf.completion(weakSelf.image);
  295. }
  296. }
  297. }];
  298. }
  299. - (void)setImage:(UIImage *)image isFromCache:(BOOL)isFromCache {
  300. self.image = image;
  301. if (!isFromCache) {
  302. CATransition *animation = [CATransition animation];
  303. [animation setDuration:0.6f];
  304. [animation setType:kCATransitionFade];
  305. animation.removedOnCompletion = YES;
  306. [self.layer addAnimation:animation forKey:@"transition"];
  307. }
  308. }
  309. - (void)cancelRequest {
  310. [self.imageDownloader.task cancel];
  311. }
  312. - (UIImage *)clipImage:(UIImage *)image toSize:(CGSize)size isScaleToMax:(BOOL)isScaleToMax {
  313. CGFloat scale = [UIScreen mainScreen].scale;
  314. UIGraphicsBeginImageContextWithOptions(size, NO, scale);
  315. CGSize aspectFitSize = CGSizeZero;
  316. if (image.size.width != 0 && image.size.height != 0) {
  317. CGFloat rateWidth = size.width / image.size.width;
  318. CGFloat rateHeight = size.height / image.size.height;
  319. CGFloat rate = isScaleToMax ? MAX(rateHeight, rateWidth) : MIN(rateHeight, rateWidth);
  320. aspectFitSize = CGSizeMake(image.size.width * rate, image.size.height * rate);
  321. }
  322. [image drawInRect:CGRectMake(0, 0, aspectFitSize.width, aspectFitSize.height)];
  323. UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
  324. UIGraphicsEndImageContext();
  325. return finalImage;
  326. }
  327. @end