UIImageView+ZFCache.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. NSError *error = nil;
  132. [[NSFileManager defaultManager] removeItemAtPath:directoryPath error:&error];
  133. [[NSFileManager defaultManager] createDirectoryAtPath:directoryPath
  134. withIntermediateDirectories:YES
  135. attributes:nil
  136. error:nil];
  137. });
  138. }
  139. [self zf_clearCache];
  140. }
  141. - (UIImage *)zf_cacheImageForRequest:(NSURLRequest *)request {
  142. if (request) {
  143. NSString *directoryPath = [NSString zf_cachePath];
  144. NSString *path = [NSString stringWithFormat:@"%@/%@", directoryPath, [NSString cachedFileNameForKey:[NSString zf_keyForRequest:request]]];
  145. return [UIImage imageWithContentsOfFile:path];
  146. }
  147. return nil;
  148. }
  149. - (NSUInteger)zf_failTimesForRequest:(NSURLRequest *)request {
  150. NSNumber *faileTimes = [self.zf_cacheFaileTimes objectForKey:[NSString cachedFileNameForKey:[NSString zf_keyForRequest:request]]];
  151. if (faileTimes && [faileTimes respondsToSelector:@selector(integerValue)]) {
  152. return faileTimes.integerValue;
  153. }
  154. return 0;
  155. }
  156. - (void)zf_cacheFailRequest:(NSURLRequest *)request {
  157. NSNumber *faileTimes = [self.zf_cacheFaileTimes objectForKey:[NSString cachedFileNameForKey:[NSString zf_keyForRequest:request]]];
  158. NSUInteger times = 0;
  159. if (faileTimes && [faileTimes respondsToSelector:@selector(integerValue)]) {
  160. times = [faileTimes integerValue];
  161. }
  162. times++;
  163. [self.zf_cacheFaileTimes setObject:@(times) forKey:[NSString cachedFileNameForKey:[NSString zf_keyForRequest:request]]];
  164. }
  165. - (void)zf_cacheImage:(UIImage *)image forRequest:(NSURLRequest *)request {
  166. if (!image || !request) { return; }
  167. NSString *directoryPath = [NSString zf_cachePath];
  168. if (![[NSFileManager defaultManager] fileExistsAtPath:directoryPath isDirectory:nil]) {
  169. NSError *error = nil;
  170. [[NSFileManager defaultManager] createDirectoryAtPath:directoryPath
  171. withIntermediateDirectories:YES
  172. attributes:nil
  173. error:&error];
  174. if (error) { return; }
  175. }
  176. NSString *path = [NSString stringWithFormat:@"%@/%@", directoryPath, [NSString cachedFileNameForKey:[NSString zf_keyForRequest:request]]];
  177. NSData *data = UIImagePNGRepresentation(image);
  178. if (data) {
  179. [[NSFileManager defaultManager] createFileAtPath:path contents:data attributes:nil];
  180. }
  181. }
  182. @end
  183. @implementation UIImageView (ZFCache)
  184. #pragma mark - getter
  185. - (ZFImageBlock)completion
  186. {
  187. return objc_getAssociatedObject(self, _cmd);
  188. }
  189. - (ZFImageDownloader *)imageDownloader
  190. {
  191. return objc_getAssociatedObject(self, _cmd);
  192. }
  193. - (NSUInteger)attemptToReloadTimesForFailedURL
  194. {
  195. NSUInteger count = [objc_getAssociatedObject(self, _cmd) integerValue];
  196. if (count == 0) { count = 2; }
  197. return count;
  198. }
  199. - (BOOL)shouldAutoClipImageToViewSize
  200. {
  201. return [objc_getAssociatedObject(self, _cmd) boolValue];
  202. }
  203. #pragma mark - setter
  204. - (void)setCompletion:(ZFImageBlock)completion
  205. {
  206. objc_setAssociatedObject(self, @selector(completion), completion, OBJC_ASSOCIATION_COPY_NONATOMIC);
  207. }
  208. - (void)setImageDownloader:(ZFImageDownloader *)imageDownloader
  209. {
  210. objc_setAssociatedObject(self, @selector(imageDownloader), imageDownloader, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  211. }
  212. - (void)setAttemptToReloadTimesForFailedURL:(NSUInteger)attemptToReloadTimesForFailedURL
  213. {
  214. objc_setAssociatedObject(self, @selector(attemptToReloadTimesForFailedURL), @(attemptToReloadTimesForFailedURL), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  215. }
  216. - (void)setShouldAutoClipImageToViewSize:(BOOL)shouldAutoClipImageToViewSize
  217. {
  218. objc_setAssociatedObject(self, @selector(shouldAutoClipImageToViewSize), @(shouldAutoClipImageToViewSize), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  219. }
  220. #pragma mark - public method
  221. - (void)setImageWithURLString:(NSString *)url
  222. placeholderImageName:(NSString *)placeholderImageName {
  223. return [self setImageWithURLString:url placeholderImageName:placeholderImageName completion:nil];
  224. }
  225. - (void)setImageWithURLString:(NSString *)url placeholder:(UIImage *)placeholderImage {
  226. return [self setImageWithURLString:url placeholder:placeholderImage completion:nil];
  227. }
  228. - (void)setImageWithURLString:(NSString *)url
  229. placeholderImageName:(NSString *)placeholderImage
  230. completion:(void (^)(UIImage *image))completion {
  231. NSString *path = [[NSBundle mainBundle] pathForResource:placeholderImage ofType:nil];
  232. UIImage *image = [UIImage imageWithContentsOfFile:path];
  233. if (image == nil) { image = [UIImage imageNamed:placeholderImage]; }
  234. [self setImageWithURLString:url placeholder:image completion:completion];
  235. }
  236. - (void)setImageWithURLString:(NSString *)url
  237. placeholder:(UIImage *)placeholderImageName
  238. completion:(void (^)(UIImage *image))completion {
  239. [self.layer removeAllAnimations];
  240. self.completion = completion;
  241. if (url == nil || [url isKindOfClass:[NSNull class]] || (![url hasPrefix:@"http://"] && ![url hasPrefix:@"https://"])) {
  242. [self setImage:placeholderImageName isFromCache:YES];
  243. if (completion) {
  244. self.completion(self.image);
  245. }
  246. return;
  247. }
  248. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
  249. [self downloadWithReqeust:request holder:placeholderImageName];
  250. }
  251. #pragma mark - private method
  252. - (void)downloadWithReqeust:(NSURLRequest *)theRequest holder:(UIImage *)holder {
  253. UIImage *cachedImage = [[UIApplication sharedApplication] zf_cacheImageForRequest:theRequest];
  254. if (cachedImage) {
  255. [self setImage:cachedImage isFromCache:YES];
  256. if (self.completion) {
  257. self.completion(cachedImage);
  258. }
  259. return;
  260. }
  261. [self setImage:holder isFromCache:YES];
  262. if ([[UIApplication sharedApplication] zf_failTimesForRequest:theRequest] >= self.attemptToReloadTimesForFailedURL) {
  263. return;
  264. }
  265. [self cancelRequest];
  266. self.imageDownloader = nil;
  267. __weak __typeof(self) weakSelf = self;
  268. self.imageDownloader = [[ZFImageDownloader alloc] init];
  269. [self.imageDownloader startDownloadImageWithUrl:theRequest.URL.absoluteString progress:nil finished:^(NSData *data, NSError *error) {
  270. // success
  271. if (data != nil && error == nil) {
  272. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  273. UIImage *image = [UIImage imageWithData:data];
  274. UIImage *finalImage = image;
  275. if (image) {
  276. if (weakSelf.shouldAutoClipImageToViewSize) {
  277. // cutting
  278. if (fabs(weakSelf.frame.size.width - image.size.width) != 0
  279. && fabs(weakSelf.frame.size.height - image.size.height) != 0) {
  280. finalImage = [self clipImage:image toSize:weakSelf.frame.size isScaleToMax:YES];
  281. }
  282. }
  283. [[UIApplication sharedApplication] zf_cacheImage:finalImage forRequest:theRequest];
  284. } else {
  285. [[UIApplication sharedApplication] zf_cacheFailRequest:theRequest];
  286. }
  287. dispatch_async(dispatch_get_main_queue(), ^{
  288. if (finalImage) {
  289. [weakSelf setImage:finalImage isFromCache:NO];
  290. if (weakSelf.completion) {
  291. weakSelf.completion(weakSelf.image);
  292. }
  293. } else {// error data
  294. if (weakSelf.completion) {
  295. weakSelf.completion(weakSelf.image);
  296. }
  297. }
  298. });
  299. });
  300. } else { // error
  301. [[UIApplication sharedApplication] zf_cacheFailRequest:theRequest];
  302. if (weakSelf.completion) {
  303. weakSelf.completion(weakSelf.image);
  304. }
  305. }
  306. }];
  307. }
  308. - (void)setImage:(UIImage *)image isFromCache:(BOOL)isFromCache {
  309. self.image = image;
  310. if (!isFromCache) {
  311. CATransition *animation = [CATransition animation];
  312. [animation setDuration:0.6f];
  313. [animation setType:kCATransitionFade];
  314. animation.removedOnCompletion = YES;
  315. [self.layer addAnimation:animation forKey:@"transition"];
  316. }
  317. }
  318. - (void)cancelRequest {
  319. [self.imageDownloader.task cancel];
  320. }
  321. - (UIImage *)clipImage:(UIImage *)image toSize:(CGSize)size isScaleToMax:(BOOL)isScaleToMax {
  322. CGFloat scale = [UIScreen mainScreen].scale;
  323. // 传入的View.frame.size是0的话,直接返回nil,防止 UIGraphicsBeginImageContext() 传入0,导致崩溃
  324. if (CGSizeEqualToSize(size, CGSizeZero)) {
  325. return nil;
  326. }
  327. UIGraphicsBeginImageContextWithOptions(size, NO, scale);
  328. CGSize aspectFitSize = CGSizeZero;
  329. if (image.size.width != 0 && image.size.height != 0) {
  330. CGFloat rateWidth = size.width / image.size.width;
  331. CGFloat rateHeight = size.height / image.size.height;
  332. CGFloat rate = isScaleToMax ? MAX(rateHeight, rateWidth) : MIN(rateHeight, rateWidth);
  333. aspectFitSize = CGSizeMake(image.size.width * rate, image.size.height * rate);
  334. }
  335. [image drawInRect:CGRectMake(0, 0, aspectFitSize.width, aspectFitSize.height)];
  336. UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
  337. UIGraphicsEndImageContext();
  338. return finalImage;
  339. }
  340. @end