LeslieAsyncImageDownloader.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // LeslieAsyncImageDownloader.m
  3. // AsyncUIImageView
  4. //
  5. // Created by Leslie.Fang on 14-8-11.
  6. // Copyright (c) 2017年 Enway. All rights reserved.
  7. //
  8. #import "LeslieAsyncImageDownloader.h"
  9. @implementation LeslieAsyncImageDownloader
  10. + (id)sharedImageLoader{
  11. static LeslieAsyncImageDownloader *sharedImageLoader = nil;
  12. static dispatch_once_t onceToken;
  13. dispatch_once(&onceToken, ^{
  14. sharedImageLoader = [[self alloc] init];
  15. });
  16. return sharedImageLoader;
  17. }
  18. - (void)downloadImageWithMultipleURL:(NSURL *)url complete:(ImageDownloadedBlock)completeBlock{
  19. LeslieImageCache *imageCache = [LeslieImageCache sharedCacheMultiple];
  20. NSString *imageUrl = [url absoluteString];
  21. // UIImage *image = [imageCache getImageFromMemoryForkey:imageUrl];
  22. // // // 先从内存中取
  23. // if (image) {
  24. // if (completeBlock) {
  25. // //NSLog(@"image exists in memory");
  26. // completeBlock(image,nil,url);
  27. // }
  28. //
  29. // return;
  30. // }
  31. // 再从文件中取
  32. // image = [imageCache getImageFromFileForKey:imageUrl];
  33. // if (image) {
  34. // if (completeBlock) {
  35. // //NSLog(@"image exists in file");
  36. // completeBlock(image,nil,url);
  37. // }
  38. //
  39. // // 重新加入到 NSCache 中
  40. // [imageCache cacheImageToMemory:image forKey:imageUrl];
  41. //
  42. // return;
  43. // }
  44. // 内存和文件中都没有再从网络下载
  45. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){
  46. NSError * error;
  47. NSData *imgData = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&error];
  48. dispatch_async(dispatch_get_main_queue(), ^{
  49. UIImage *image = [UIImage imageWithData:imgData];
  50. if (image) {
  51. // 先缓存图片到内存
  52. // [imageCache cacheImageToMemory:image forKey:imageUrl];
  53. // 再缓存图片到文件系统
  54. NSString *extension = [[imageUrl substringFromIndex:imageUrl.length-3] lowercaseString];
  55. NSString *imageType = @"jpg";
  56. if ([extension isEqualToString:@"jpg"]) {
  57. imageType = @"jpg";
  58. }else{
  59. imageType = @"png";
  60. }
  61. [imageCache cacheImageToFile:image forKey:imageUrl ofType:imageType];
  62. }
  63. if (completeBlock) {
  64. completeBlock(image,error,url);
  65. }
  66. });
  67. });
  68. }
  69. - (void)downloadImageWithURL:(NSURL *)url complete:(ImageDownloadedBlock)completeBlock{
  70. LeslieImageCache *imageCache = [LeslieImageCache sharedCache];
  71. NSString *imageUrl = [url absoluteString];
  72. UIImage *image = [imageCache getImageFromMemoryForkey:imageUrl];
  73. // // 先从内存中取
  74. if (image) {
  75. if (completeBlock) {
  76. NSLog(@"image exists in memory");
  77. completeBlock(image,nil,url);
  78. }
  79. return;
  80. }
  81. // 再从文件中取
  82. image = [imageCache getImageFromFileForKey:imageUrl];
  83. if (image) {
  84. if (completeBlock) {
  85. NSLog(@"image exists in file");
  86. completeBlock(image,nil,url);
  87. }
  88. // 重新加入到 NSCache 中
  89. [imageCache cacheImageToMemory:image forKey:imageUrl];
  90. return;
  91. }
  92. // 内存和文件中都没有再从网络下载
  93. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){
  94. NSError * error;
  95. NSData *imgData = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&error];
  96. dispatch_async(dispatch_get_main_queue(), ^{
  97. UIImage *image = [UIImage imageWithData:imgData];
  98. if (image) {
  99. // 先缓存图片到内存
  100. [imageCache cacheImageToMemory:image forKey:imageUrl];
  101. // 再缓存图片到文件系统
  102. NSString *extension = [[imageUrl substringFromIndex:imageUrl.length-3] lowercaseString];
  103. NSString *imageType = @"jpg";
  104. if ([extension isEqualToString:@"jpg"]) {
  105. imageType = @"jpg";
  106. }else{
  107. imageType = @"png";
  108. }
  109. [imageCache cacheImageToFile:image forKey:imageUrl ofType:imageType];
  110. }
  111. if (completeBlock) {
  112. completeBlock(image,error,url);
  113. }
  114. });
  115. });
  116. }
  117. -(void)downloadFileWithURL:(NSURL *)url filePath:(NSString*)path
  118. {
  119. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){
  120. NSError * error;
  121. NSData *fileData = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&error];
  122. dispatch_async(dispatch_get_main_queue(), ^{
  123. if(fileData)
  124. {
  125. [fileData writeToFile:path atomically:YES];
  126. }
  127. });
  128. });
  129. }
  130. + (NSData *)resetSizeOfImageData:(UIImage *)source_image maxSize:(NSInteger)maxSize
  131. {
  132. //先调整分辨率
  133. CGSize newSize = CGSizeMake(source_image.size.width, source_image.size.height);
  134. CGFloat tempHeight = newSize.height / 1024;
  135. CGFloat tempWidth = newSize.width / 1024;
  136. if (tempWidth > 1.0 && tempWidth > tempHeight) {
  137. newSize = CGSizeMake(source_image.size.width / tempWidth, source_image.size.height / tempWidth);
  138. }
  139. else if (tempHeight > 1.0 && tempWidth < tempHeight){
  140. newSize = CGSizeMake(source_image.size.width / tempHeight, source_image.size.height / tempHeight);
  141. }
  142. UIGraphicsBeginImageContext(newSize);
  143. [source_image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  144. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  145. UIGraphicsEndImageContext();
  146. //调整大小
  147. NSData *imageData = UIImageJPEGRepresentation(newImage,1.0);
  148. NSUInteger sizeOrigin = [imageData length];
  149. NSUInteger sizeOriginKB = sizeOrigin / 1024;
  150. if (sizeOriginKB > maxSize) {
  151. NSData *finallImageData = UIImageJPEGRepresentation(newImage,0.5);
  152. return finallImageData;
  153. }
  154. return imageData;
  155. }
  156. @end