UIButton+WebCache.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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
  10. #import "SDWebImageManager.h"
  11. /**
  12. * Integrates SDWebImage async downloading and caching of remote images with UIButton.
  13. */
  14. @interface UIButton (WebCache)
  15. #pragma mark - Image
  16. /**
  17. * Get the current image URL.
  18. */
  19. @property (nonatomic, strong, readonly, nullable) NSURL *sd_currentImageURL;
  20. /**
  21. * Get the image URL for a control state.
  22. *
  23. * @param state Which state you want to know the URL for. The values are described in UIControlState.
  24. */
  25. - (nullable NSURL *)sd_imageURLForState:(UIControlState)state;
  26. /**
  27. * Set the button `image` with an `url`.
  28. *
  29. * The download is asynchronous and cached.
  30. *
  31. * @param url The url for the image.
  32. * @param state The state that uses the specified title. The values are described in UIControlState.
  33. */
  34. - (void)sd_setImageWithURL:(nullable NSURL *)url
  35. forState:(UIControlState)state NS_REFINED_FOR_SWIFT;
  36. /**
  37. * Set the button `image` with an `url` and a placeholder.
  38. *
  39. * The download is asynchronous and cached.
  40. *
  41. * @param url The url for the image.
  42. * @param state The state that uses the specified title. The values are described in UIControlState.
  43. * @param placeholder The image to be set initially, until the image request finishes.
  44. * @see sd_setImageWithURL:placeholderImage:options:
  45. */
  46. - (void)sd_setImageWithURL:(nullable NSURL *)url
  47. forState:(UIControlState)state
  48. placeholderImage:(nullable UIImage *)placeholder NS_REFINED_FOR_SWIFT;
  49. /**
  50. * Set the button `image` with an `url`, placeholder and custom options.
  51. *
  52. * The download is asynchronous and cached.
  53. *
  54. * @param url The url for the image.
  55. * @param state The state that uses the specified title. The values are described in UIControlState.
  56. * @param placeholder The image to be set initially, until the image request finishes.
  57. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  58. */
  59. - (void)sd_setImageWithURL:(nullable NSURL *)url
  60. forState:(UIControlState)state
  61. placeholderImage:(nullable UIImage *)placeholder
  62. options:(SDWebImageOptions)options NS_REFINED_FOR_SWIFT;
  63. /**
  64. * Set the button `image` with an `url`, placeholder, custom options and context.
  65. *
  66. * The download is asynchronous and cached.
  67. *
  68. * @param url The url for the image.
  69. * @param state The state that uses the specified title. The values are described in UIControlState.
  70. * @param placeholder The image to be set initially, until the image request finishes.
  71. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  72. * @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.
  73. */
  74. - (void)sd_setImageWithURL:(nullable NSURL *)url
  75. forState:(UIControlState)state
  76. placeholderImage:(nullable UIImage *)placeholder
  77. options:(SDWebImageOptions)options
  78. context:(nullable SDWebImageContext *)context;
  79. /**
  80. * Set the button `image` with an `url`.
  81. *
  82. * The download is asynchronous and cached.
  83. *
  84. * @param url The url for the image.
  85. * @param state The state that uses the specified title. The values are described in UIControlState.
  86. * @param completedBlock A block called when operation has been completed. This block has no return value
  87. * and takes the requested UIImage as first parameter. In case of error the image parameter
  88. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  89. * indicating if the image was retrieved from the local cache or from the network.
  90. * The fourth parameter is the original image url.
  91. */
  92. - (void)sd_setImageWithURL:(nullable NSURL *)url
  93. forState:(UIControlState)state
  94. completed:(nullable SDExternalCompletionBlock)completedBlock;
  95. /**
  96. * Set the button `image` with an `url`, placeholder.
  97. *
  98. * The download is asynchronous and cached.
  99. *
  100. * @param url The url for the image.
  101. * @param state The state that uses the specified title. The values are described in UIControlState.
  102. * @param placeholder The image to be set initially, until the image request finishes.
  103. * @param completedBlock A block called when operation has been completed. This block has no return value
  104. * and takes the requested UIImage as first parameter. In case of error the image parameter
  105. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  106. * indicating if the image was retrieved from the local cache or from the network.
  107. * The fourth parameter is the original image url.
  108. */
  109. - (void)sd_setImageWithURL:(nullable NSURL *)url
  110. forState:(UIControlState)state
  111. placeholderImage:(nullable UIImage *)placeholder
  112. completed:(nullable SDExternalCompletionBlock)completedBlock NS_REFINED_FOR_SWIFT;
  113. /**
  114. * Set the button `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 state The state that uses the specified title. The values are described in UIControlState.
  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 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:(nullable NSURL *)url
  129. forState:(UIControlState)state
  130. placeholderImage:(nullable UIImage *)placeholder
  131. options:(SDWebImageOptions)options
  132. completed:(nullable SDExternalCompletionBlock)completedBlock;
  133. /**
  134. * Set the button `image` with an `url`, placeholder and custom options.
  135. *
  136. * The download is asynchronous and cached.
  137. *
  138. * @param url The url for the image.
  139. * @param state The state that uses the specified title. The values are described in UIControlState.
  140. * @param placeholder The image to be set initially, until the image request finishes.
  141. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  142. * @param progressBlock A block called while image is downloading
  143. * @note the progress block is executed on a background queue
  144. * @param completedBlock A block called when operation has been completed. This block has no return value
  145. * and takes the requested UIImage as first parameter. In case of error the image parameter
  146. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  147. * indicating if the image was retrieved from the local cache or from the network.
  148. * The fourth parameter is the original image url.
  149. */
  150. - (void)sd_setImageWithURL:(nullable NSURL *)url
  151. forState:(UIControlState)state
  152. placeholderImage:(nullable UIImage *)placeholder
  153. options:(SDWebImageOptions)options
  154. progress:(nullable SDImageLoaderProgressBlock)progressBlock
  155. completed:(nullable SDExternalCompletionBlock)completedBlock;
  156. /**
  157. * Set the button `image` with an `url`, placeholder, custom options and context.
  158. *
  159. * The download is asynchronous and cached.
  160. *
  161. * @param url The url for the image.
  162. * @param state The state that uses the specified title. The values are described in UIControlState.
  163. * @param placeholder The image to be set initially, until the image request finishes.
  164. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  165. * @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.
  166. * @param progressBlock A block called while image is downloading
  167. * @note the progress block is executed on a background queue
  168. * @param completedBlock A block called when operation has been completed. This block has no return value
  169. * and takes the requested UIImage as first parameter. In case of error the image parameter
  170. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  171. * indicating if the image was retrieved from the local cache or from the network.
  172. * The fourth parameter is the original image url.
  173. */
  174. - (void)sd_setImageWithURL:(nullable NSURL *)url
  175. forState:(UIControlState)state
  176. placeholderImage:(nullable UIImage *)placeholder
  177. options:(SDWebImageOptions)options
  178. context:(nullable SDWebImageContext *)context
  179. progress:(nullable SDImageLoaderProgressBlock)progressBlock
  180. completed:(nullable SDExternalCompletionBlock)completedBlock;
  181. #pragma mark - Background Image
  182. /**
  183. * Get the current background image URL.
  184. */
  185. @property (nonatomic, strong, readonly, nullable) NSURL *sd_currentBackgroundImageURL;
  186. /**
  187. * Get the background image URL for a control state.
  188. *
  189. * @param state Which state you want to know the URL for. The values are described in UIControlState.
  190. */
  191. - (nullable NSURL *)sd_backgroundImageURLForState:(UIControlState)state;
  192. /**
  193. * Set the button `backgroundImage` with an `url`.
  194. *
  195. * The download is asynchronous and cached.
  196. *
  197. * @param url The url for the image.
  198. * @param state The state that uses the specified title. The values are described in UIControlState.
  199. */
  200. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url
  201. forState:(UIControlState)state NS_REFINED_FOR_SWIFT;
  202. /**
  203. * Set the button `backgroundImage` with an `url` and a placeholder.
  204. *
  205. * The download is asynchronous and cached.
  206. *
  207. * @param url The url for the image.
  208. * @param state The state that uses the specified title. The values are described in UIControlState.
  209. * @param placeholder The image to be set initially, until the image request finishes.
  210. * @see sd_setImageWithURL:placeholderImage:options:
  211. */
  212. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url
  213. forState:(UIControlState)state
  214. placeholderImage:(nullable UIImage *)placeholder NS_REFINED_FOR_SWIFT;
  215. /**
  216. * Set the button `backgroundImage` with an `url`, placeholder and custom options.
  217. *
  218. * The download is asynchronous and cached.
  219. *
  220. * @param url The url for the image.
  221. * @param state The state that uses the specified title. The values are described in UIControlState.
  222. * @param placeholder The image to be set initially, until the image request finishes.
  223. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  224. */
  225. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url
  226. forState:(UIControlState)state
  227. placeholderImage:(nullable UIImage *)placeholder
  228. options:(SDWebImageOptions)options NS_REFINED_FOR_SWIFT;
  229. /**
  230. * Set the button `backgroundImage` with an `url`, placeholder, custom options and context.
  231. *
  232. * The download is asynchronous and cached.
  233. *
  234. * @param url The url for the image.
  235. * @param state The state that uses the specified title. The values are described in UIControlState.
  236. * @param placeholder The image to be set initially, until the image request finishes.
  237. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  238. * @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.
  239. */
  240. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url
  241. forState:(UIControlState)state
  242. placeholderImage:(nullable UIImage *)placeholder
  243. options:(SDWebImageOptions)options
  244. context:(nullable SDWebImageContext *)context;
  245. /**
  246. * Set the button `backgroundImage` with an `url`.
  247. *
  248. * The download is asynchronous and cached.
  249. *
  250. * @param url The url for the image.
  251. * @param state The state that uses the specified title. The values are described in UIControlState.
  252. * @param completedBlock A block called when operation has been completed. This block has no return value
  253. * and takes the requested UIImage as first parameter. In case of error the image parameter
  254. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  255. * indicating if the image was retrieved from the local cache or from the network.
  256. * The fourth parameter is the original image url.
  257. */
  258. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url
  259. forState:(UIControlState)state
  260. completed:(nullable SDExternalCompletionBlock)completedBlock;
  261. /**
  262. * Set the button `backgroundImage` with an `url`, placeholder.
  263. *
  264. * The download is asynchronous and cached.
  265. *
  266. * @param url The url for the image.
  267. * @param state The state that uses the specified title. The values are described in UIControlState.
  268. * @param placeholder The image to be set initially, until the image request finishes.
  269. * @param completedBlock A block called when operation has been completed. This block has no return value
  270. * and takes the requested UIImage as first parameter. In case of error the image parameter
  271. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  272. * indicating if the image was retrieved from the local cache or from the network.
  273. * The fourth parameter is the original image url.
  274. */
  275. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url
  276. forState:(UIControlState)state
  277. placeholderImage:(nullable UIImage *)placeholder
  278. completed:(nullable SDExternalCompletionBlock)completedBlock NS_REFINED_FOR_SWIFT;
  279. /**
  280. * Set the button `backgroundImage` with an `url`, placeholder and custom options.
  281. *
  282. * The download is asynchronous and cached.
  283. *
  284. * @param url The url for the image.
  285. * @param placeholder The image to be set initially, until the image request finishes.
  286. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  287. * @param completedBlock A block called when operation has been completed. This block has no return value
  288. * and takes the requested UIImage as first parameter. In case of error the image parameter
  289. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  290. * indicating if the image was retrieved from the local cache or from the network.
  291. * The fourth parameter is the original image url.
  292. */
  293. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url
  294. forState:(UIControlState)state
  295. placeholderImage:(nullable UIImage *)placeholder
  296. options:(SDWebImageOptions)options
  297. completed:(nullable SDExternalCompletionBlock)completedBlock;
  298. /**
  299. * Set the button `backgroundImage` with an `url`, placeholder and custom options.
  300. *
  301. * The download is asynchronous and cached.
  302. *
  303. * @param url The url for the image.
  304. * @param placeholder The image to be set initially, until the image request finishes.
  305. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  306. * @param progressBlock A block called while image is downloading
  307. * @note the progress block is executed on a background queue
  308. * @param completedBlock A block called when operation has been completed. This block has no return value
  309. * and takes the requested UIImage as first parameter. In case of error the image parameter
  310. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  311. * indicating if the image was retrieved from the local cache or from the network.
  312. * The fourth parameter is the original image url.
  313. */
  314. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url
  315. forState:(UIControlState)state
  316. placeholderImage:(nullable UIImage *)placeholder
  317. options:(SDWebImageOptions)options
  318. progress:(nullable SDImageLoaderProgressBlock)progressBlock
  319. completed:(nullable SDExternalCompletionBlock)completedBlock;
  320. /**
  321. * Set the button `backgroundImage` with an `url`, placeholder, custom options and context.
  322. *
  323. * The download is asynchronous and cached.
  324. *
  325. * @param url The url for the image.
  326. * @param placeholder The image to be set initially, until the image request finishes.
  327. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  328. * @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.
  329. * @param progressBlock A block called while image is downloading
  330. * @note the progress block is executed on a background queue
  331. * @param completedBlock A block called when operation has been completed. This block has no return value
  332. * and takes the requested UIImage as first parameter. In case of error the image parameter
  333. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  334. * indicating if the image was retrieved from the local cache or from the network.
  335. * The fourth parameter is the original image url.
  336. */
  337. - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url
  338. forState:(UIControlState)state
  339. placeholderImage:(nullable UIImage *)placeholder
  340. options:(SDWebImageOptions)options
  341. context:(nullable SDWebImageContext *)context
  342. progress:(nullable SDImageLoaderProgressBlock)progressBlock
  343. completed:(nullable SDExternalCompletionBlock)completedBlock;
  344. #pragma mark - Cancel
  345. /**
  346. * Cancel the current image download
  347. */
  348. - (void)sd_cancelImageLoadForState:(UIControlState)state;
  349. /**
  350. * Cancel the current backgroundImage download
  351. */
  352. - (void)sd_cancelBackgroundImageLoadForState:(UIControlState)state;
  353. @end
  354. #endif