UIImageView+WebCache.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. autorelease];
  25. }
  26. // Here we use the provided sd_setImageWithURL: method to load the web image
  27. // Ensure you use a placeholder image otherwise cells will be initialized with no image
  28. [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"]
  29. placeholderImage:[UIImage imageNamed:@"placeholder"]];
  30. cell.textLabel.text = @"My Text";
  31. return cell;
  32. }
  33. * @endcode
  34. */
  35. @interface UIImageView (WebCache)
  36. /**
  37. * Get the current image URL.
  38. *
  39. * Note that because of the limitations of categories this property can get out of sync
  40. * if you use sd_setImage: directly.
  41. */
  42. - (NSURL *)sd_imageURL;
  43. /**
  44. * Set the imageView `image` with an `url`.
  45. *
  46. * The download is asynchronous and cached.
  47. *
  48. * @param url The url for the image.
  49. */
  50. - (void)sd_setImageWithURL:(NSURL *)url;
  51. /**
  52. * Set the imageView `image` with an `url` and a placeholder.
  53. *
  54. * The download is asynchronous and cached.
  55. *
  56. * @param url The url for the image.
  57. * @param placeholder The image to be set initially, until the image request finishes.
  58. * @see sd_setImageWithURL:placeholderImage:options:
  59. */
  60. - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder;
  61. /**
  62. * Set the imageView `image` with an `url`, placeholder and custom options.
  63. *
  64. * The download is asynchronous and cached.
  65. *
  66. * @param url The url for the image.
  67. * @param placeholder The image to be set initially, until the image request finishes.
  68. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  69. */
  70. - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options;
  71. /**
  72. * Set the imageView `image` with an `url`.
  73. *
  74. * The download is asynchronous and cached.
  75. *
  76. * @param url The url for the image.
  77. * @param completedBlock A block called when operation has been completed. This block has no return value
  78. * and takes the requested UIImage as first parameter. In case of error the image parameter
  79. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  80. * indicating if the image was retrieved from the local cache or from the network.
  81. * The fourth parameter is the original image url.
  82. */
  83. - (void)sd_setImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock;
  84. /**
  85. * Set the imageView `image` with an `url`, placeholder.
  86. *
  87. * The download is asynchronous and cached.
  88. *
  89. * @param url The url for the image.
  90. * @param placeholder The image to be set initially, until the image request finishes.
  91. * @param completedBlock A block called when operation has been completed. This block has no return value
  92. * and takes the requested UIImage as first parameter. In case of error the image parameter
  93. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  94. * indicating if the image was retrieved from the local cache or from the network.
  95. * The fourth parameter is the original image url.
  96. */
  97. - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock;
  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:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock;
  113. /**
  114. * Set the imageView `image` with an `url`, placeholder and custom options.
  115. *
  116. * The download is asynchronous and cached.
  117. *
  118. * @param url The url for the image.
  119. * @param placeholder The image to be set initially, until the image request finishes.
  120. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  121. * @param progressBlock A block called while image is downloading
  122. * @param completedBlock A block called when operation has been completed. This block has no return value
  123. * and takes the requested UIImage as first parameter. In case of error the image parameter
  124. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  125. * indicating if the image was retrieved from the local cache or from the network.
  126. * The fourth parameter is the original image url.
  127. */
  128. - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock;
  129. /**
  130. * Set the imageView `image` with an `url` and optionally a placeholder image.
  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. * @param completedBlock A block called when operation has been completed. This block has no return value
  139. * and takes the requested UIImage as first parameter. In case of error the image parameter
  140. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  141. * indicating if the image was retrieved from the local cache or from the network.
  142. * The fourth parameter is the original image url.
  143. */
  144. - (void)sd_setImageWithPreviousCachedImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock;
  145. /**
  146. * Download an array of images and starts them in an animation loop
  147. *
  148. * @param arrayOfURLs An array of NSURL
  149. */
  150. - (void)sd_setAnimationImagesWithURLs:(NSArray *)arrayOfURLs;
  151. /**
  152. * Cancel the current download
  153. */
  154. - (void)sd_cancelCurrentImageLoad;
  155. - (void)sd_cancelCurrentAnimationImagesLoad;
  156. /**
  157. * Show activity UIActivityIndicatorView
  158. */
  159. - (void)setShowActivityIndicatorView:(BOOL)show;
  160. /**
  161. * set desired UIActivityIndicatorViewStyle
  162. *
  163. * @param style The style of the UIActivityIndicatorView
  164. */
  165. - (void)setIndicatorStyle:(UIActivityIndicatorViewStyle)style;
  166. @end
  167. @interface UIImageView (WebCacheDeprecated)
  168. - (NSURL *)imageURL __deprecated_msg("Use `sd_imageURL`");
  169. - (void)setImageWithURL:(NSURL *)url __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:`");
  170. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:placeholderImage:`");
  171. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:placeholderImage:options`");
  172. - (void)setImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:completed:`");
  173. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:placeholderImage:completed:`");
  174. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:placeholderImage:options:completed:`");
  175. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setImageWithURL:placeholderImage:options:progress:completed:`");
  176. - (void)sd_setImageWithPreviousCachedImageWithURL:(NSURL *)url andPlaceholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock __deprecated_msg("Method deprecated. Use `sd_setImageWithPreviousCachedImageWithURL:placeholderImage:options:progress:completed:`");
  177. - (void)setAnimationImagesWithURLs:(NSArray *)arrayOfURLs __deprecated_msg("Use `sd_setAnimationImagesWithURLs:`");
  178. - (void)cancelCurrentArrayLoad __deprecated_msg("Use `sd_cancelCurrentAnimationImagesLoad`");
  179. - (void)cancelCurrentImageLoad __deprecated_msg("Use `sd_cancelCurrentImageLoad`");
  180. @end