UIImageView+WebCache.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #if SD_UIKIT || SD_MAC
  10. #import "SDWebImageManager.h"
  11. /**
  12. * Integrates SDWebImage async downloading and caching of remote images with UIImageView.
  13. *
  14. * Usage with a UITableViewCell sub-class:
  15. *
  16. * @code
  17. #import <SDWebImage/UIImageView+WebCache.h>
  18. ...
  19. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  20. {
  21. static NSString *MyIdentifier = @"MyIdentifier";
  22. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
  23. if (cell == nil) {
  24. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]
  25. autorelease];
  26. }
  27. // Here we use the provided sd_setImageWithURL: method to load the web image
  28. // Ensure you use a placeholder image otherwise cells will be initialized with no image
  29. [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"]
  30. placeholderImage:[UIImage imageNamed:@"placeholder"]];
  31. cell.textLabel.text = @"My Text";
  32. return cell;
  33. }
  34. * @endcode
  35. */
  36. @interface UIImageView (WebCache)
  37. /**
  38. * Set the imageView `image` with an `url`.
  39. *
  40. * The download is asynchronous and cached.
  41. *
  42. * @param url The url for the image.
  43. */
  44. - (void)sd_setImageWithURL:(nullable NSURL *)url NS_REFINED_FOR_SWIFT;
  45. /**
  46. * Set the imageView `image` with an `url` and a placeholder.
  47. *
  48. * The download is asynchronous and cached.
  49. *
  50. * @param url The url for the image.
  51. * @param placeholder The image to be set initially, until the image request finishes.
  52. * @see sd_setImageWithURL:placeholderImage:options:
  53. */
  54. - (void)sd_setImageWithURL:(nullable NSURL *)url
  55. placeholderImage:(nullable UIImage *)placeholder NS_REFINED_FOR_SWIFT;
  56. /**
  57. * Set the imageView `image` with an `url`, placeholder and custom options.
  58. *
  59. * The download is asynchronous and cached.
  60. *
  61. * @param url The url for the image.
  62. * @param placeholder The image to be set initially, until the image request finishes.
  63. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  64. */
  65. - (void)sd_setImageWithURL:(nullable NSURL *)url
  66. placeholderImage:(nullable UIImage *)placeholder
  67. options:(SDWebImageOptions)options NS_REFINED_FOR_SWIFT;
  68. /**
  69. * Set the imageView `image` with an `url`.
  70. *
  71. * The download is asynchronous and cached.
  72. *
  73. * @param url The url for the image.
  74. * @param completedBlock A block called when operation has been completed. This block has no return value
  75. * and takes the requested UIImage as first parameter. In case of error the image parameter
  76. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  77. * indicating if the image was retrieved from the local cache or from the network.
  78. * The fourth parameter is the original image url.
  79. */
  80. - (void)sd_setImageWithURL:(nullable NSURL *)url
  81. completed:(nullable SDExternalCompletionBlock)completedBlock;
  82. /**
  83. * Set the imageView `image` with an `url`, placeholder.
  84. *
  85. * The download is asynchronous and cached.
  86. *
  87. * @param url The url for the image.
  88. * @param placeholder The image to be set initially, until the image request finishes.
  89. * @param completedBlock A block called when operation has been completed. This block has no return value
  90. * and takes the requested UIImage as first parameter. In case of error the image parameter
  91. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  92. * indicating if the image was retrieved from the local cache or from the network.
  93. * The fourth parameter is the original image url.
  94. */
  95. - (void)sd_setImageWithURL:(nullable NSURL *)url
  96. placeholderImage:(nullable UIImage *)placeholder
  97. completed:(nullable SDExternalCompletionBlock)completedBlock NS_REFINED_FOR_SWIFT;
  98. /**
  99. * Set the imageView `image` with an `url`, placeholder and custom options.
  100. *
  101. * The download is asynchronous and cached.
  102. *
  103. * @param url The url for the image.
  104. * @param placeholder The image to be set initially, until the image request finishes.
  105. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  106. * @param completedBlock A block called when operation has been completed. This block has no return value
  107. * and takes the requested UIImage as first parameter. In case of error the image parameter
  108. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  109. * indicating if the image was retrieved from the local cache or from the network.
  110. * The fourth parameter is the original image url.
  111. */
  112. - (void)sd_setImageWithURL:(nullable NSURL *)url
  113. placeholderImage:(nullable UIImage *)placeholder
  114. options:(SDWebImageOptions)options
  115. completed:(nullable SDExternalCompletionBlock)completedBlock;
  116. /**
  117. * Set the imageView `image` with an `url`, placeholder and custom options.
  118. *
  119. * The download is asynchronous and cached.
  120. *
  121. * @param url The url for the image.
  122. * @param placeholder The image to be set initially, until the image request finishes.
  123. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  124. * @param progressBlock A block called while image is downloading
  125. * @note the progress block is executed on a background queue
  126. * @param completedBlock A block called when operation has been completed. This block has no return value
  127. * and takes the requested UIImage as first parameter. In case of error the image parameter
  128. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  129. * indicating if the image was retrieved from the local cache or from the network.
  130. * The fourth parameter is the original image url.
  131. */
  132. - (void)sd_setImageWithURL:(nullable NSURL *)url
  133. placeholderImage:(nullable UIImage *)placeholder
  134. options:(SDWebImageOptions)options
  135. progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  136. completed:(nullable SDExternalCompletionBlock)completedBlock;
  137. /**
  138. * Set the imageView `image` with an `url` and optionally a placeholder image.
  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. */
  153. - (void)sd_setImageWithPreviousCachedImageWithURL:(nullable NSURL *)url
  154. placeholderImage:(nullable UIImage *)placeholder
  155. options:(SDWebImageOptions)options
  156. progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  157. completed:(nullable SDExternalCompletionBlock)completedBlock;
  158. #if SD_UIKIT
  159. #pragma mark - Animation of multiple images
  160. /**
  161. * Download an array of images and starts them in an animation loop
  162. *
  163. * @param arrayOfURLs An array of NSURL
  164. */
  165. - (void)sd_setAnimationImagesWithURLs:(nonnull NSArray<NSURL *> *)arrayOfURLs;
  166. - (void)sd_cancelCurrentAnimationImagesLoad;
  167. #endif
  168. @end
  169. #endif