UIImageView+WebCache.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import "SDWebImageCompat.h"
  9. #import "SDWebImageManager.h"
  10. /**
  11. * Integrates SDWebImage async downloading and caching of remote images with UIImageView.
  12. *
  13. * Usage with a UITableViewCell sub-class:
  14. *
  15. * @code
  16. #import <SDWebImage/UIImageView+WebCache.h>
  17. ...
  18. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  19. {
  20. static NSString *MyIdentifier = @"MyIdentifier";
  21. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
  22. if (cell == nil) {
  23. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
  24. }
  25. // Here we use the provided sd_setImageWithURL: method to load the web image
  26. // Ensure you use a placeholder image otherwise cells will be initialized with no image
  27. [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"]
  28. placeholderImage:[UIImage imageNamed:@"placeholder"]];
  29. cell.textLabel.text = @"My Text";
  30. return cell;
  31. }
  32. * @endcode
  33. */
  34. @interface UIImageView (WebCache)
  35. /**
  36. * Set the imageView `image` with an `url`.
  37. *
  38. * The download is asynchronous and cached.
  39. *
  40. * @param url The url for the image.
  41. */
  42. - (void)sd_setImageWithURL:(nullable NSURL *)url NS_REFINED_FOR_SWIFT;
  43. /**
  44. * Set the imageView `image` with an `url` and a placeholder.
  45. *
  46. * The download is asynchronous and cached.
  47. *
  48. * @param url The url for the image.
  49. * @param placeholder The image to be set initially, until the image request finishes.
  50. * @see sd_setImageWithURL:placeholderImage:options:
  51. */
  52. - (void)sd_setImageWithURL:(nullable NSURL *)url
  53. placeholderImage:(nullable UIImage *)placeholder NS_REFINED_FOR_SWIFT;
  54. /**
  55. * Set the imageView `image` with an `url`, placeholder and custom options.
  56. *
  57. * The download is asynchronous and cached.
  58. *
  59. * @param url The url for the image.
  60. * @param placeholder The image to be set initially, until the image request finishes.
  61. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  62. */
  63. - (void)sd_setImageWithURL:(nullable NSURL *)url
  64. placeholderImage:(nullable UIImage *)placeholder
  65. options:(SDWebImageOptions)options NS_REFINED_FOR_SWIFT;
  66. /**
  67. * Set the imageView `image` with an `url`.
  68. *
  69. * The download is asynchronous and cached.
  70. *
  71. * @param url The url for the image.
  72. * @param completedBlock A block called when operation has been completed. This block has no return value
  73. * and takes the requested UIImage as first parameter. In case of error the image parameter
  74. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  75. * indicating if the image was retrieved from the local cache or from the network.
  76. * The fourth parameter is the original image url.
  77. */
  78. - (void)sd_setImageWithURL:(nullable NSURL *)url
  79. completed:(nullable SDExternalCompletionBlock)completedBlock;
  80. /**
  81. * Set the imageView `image` with an `url`, placeholder.
  82. *
  83. * The download is asynchronous and cached.
  84. *
  85. * @param url The url for the image.
  86. * @param placeholder The image to be set initially, until the image request finishes.
  87. * @param completedBlock A block called when operation has been completed. This block has no return value
  88. * and takes the requested UIImage as first parameter. In case of error the image parameter
  89. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  90. * indicating if the image was retrieved from the local cache or from the network.
  91. * The fourth parameter is the original image url.
  92. */
  93. - (void)sd_setImageWithURL:(nullable NSURL *)url
  94. placeholderImage:(nullable UIImage *)placeholder
  95. completed:(nullable SDExternalCompletionBlock)completedBlock NS_REFINED_FOR_SWIFT;
  96. /**
  97. * Set the imageView `image` with an `url`, placeholder and custom options.
  98. *
  99. * The download is asynchronous and cached.
  100. *
  101. * @param url The url for the image.
  102. * @param placeholder The image to be set initially, until the image request finishes.
  103. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  104. * @param completedBlock A block called when operation has been completed. This block has no return value
  105. * and takes the requested UIImage as first parameter. In case of error the image parameter
  106. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  107. * indicating if the image was retrieved from the local cache or from the network.
  108. * The fourth parameter is the original image url.
  109. */
  110. - (void)sd_setImageWithURL:(nullable NSURL *)url
  111. placeholderImage:(nullable UIImage *)placeholder
  112. options:(SDWebImageOptions)options
  113. completed:(nullable SDExternalCompletionBlock)completedBlock;
  114. /**
  115. * Set the imageView `image` with an `url`, placeholder and custom options.
  116. *
  117. * The download is asynchronous and cached.
  118. *
  119. * @param url The url for the image.
  120. * @param placeholder The image to be set initially, until the image request finishes.
  121. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  122. * @param progressBlock A block called while image is downloading
  123. * @note the progress block is executed on a background queue
  124. * @param completedBlock A block called when operation has been completed. This block has no return value
  125. * and takes the requested UIImage as first parameter. In case of error the image parameter
  126. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  127. * indicating if the image was retrieved from the local cache or from the network.
  128. * The fourth parameter is the original image url.
  129. */
  130. - (void)sd_setImageWithURL:(nullable NSURL *)url
  131. placeholderImage:(nullable UIImage *)placeholder
  132. options:(SDWebImageOptions)options
  133. progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  134. completed:(nullable SDExternalCompletionBlock)completedBlock;
  135. /**
  136. * Set the imageView `image` with an `url` and custom options. The placeholder image is from previous cached image and will use the provided one instead if the query failed.
  137. * This method was designed to ensure that placeholder and query cache process happened in the same runloop to avoid flashing on cell during two `setImage:` call. But it's really misunderstanding and deprecated.
  138. * This can be done by using `sd_setImageWithURL:` with `SDWebImageQueryDiskSync`. But take care that if the memory cache missed, query disk cache synchronously may reduce the frame rate
  139. *
  140. * The download is asynchronous and cached.
  141. *
  142. * @param url The url for the image.
  143. * @param placeholder The image to be set initially, until the image request finishes.
  144. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  145. * @param progressBlock A block called while image is downloading
  146. * @note the progress block is executed on a background queue
  147. * @param completedBlock A block called when operation has been completed. This block has no return value
  148. * and takes the requested UIImage as first parameter. In case of error the image parameter
  149. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  150. * indicating if the image was retrieved from the local cache or from the network.
  151. * The fourth parameter is the original image url.
  152. * @deprecated consider using `SDWebImageQueryDiskSync` options with `sd_setImageWithURL:` instead
  153. */
  154. - (void)sd_setImageWithPreviousCachedImageWithURL:(nullable NSURL *)url
  155. placeholderImage:(nullable UIImage *)placeholder
  156. options:(SDWebImageOptions)options
  157. progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  158. completed:(nullable SDExternalCompletionBlock)completedBlock __deprecated_msg("This method is misunderstanding and deprecated, consider using `SDWebImageQueryDiskSync` options with `sd_setImageWithURL:` instead");
  159. #if SD_UIKIT
  160. #pragma mark - Animation of multiple images
  161. /**
  162. * Download an array of images and starts them in an animation loop
  163. *
  164. * @param arrayOfURLs An array of NSURL
  165. */
  166. - (void)sd_setAnimationImagesWithURLs:(nonnull NSArray<NSURL *> *)arrayOfURLs;
  167. - (void)sd_cancelCurrentAnimationImagesLoad;
  168. #endif
  169. @end