UIImageView+WebCache.h 9.1 KB

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