NSButton+WebCache.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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_MAC
  10. #import "SDWebImageManager.h"
  11. /**
  12. * Integrates SDWebImage async downloading and caching of remote images with NSButton.
  13. */
  14. @interface NSButton (WebCache)
  15. #pragma mark - Image
  16. /**
  17. * Get the current image URL.
  18. */
  19. @property (nonatomic, strong, readonly, nullable) NSURL *sd_currentImageURL;
  20. /**
  21. * Set the button `image` with an `url`.
  22. *
  23. * The download is asynchronous and cached.
  24. *
  25. * @param url The url for the image.
  26. */
  27. - (void)sd_setImageWithURL:(nullable NSURL *)url NS_REFINED_FOR_SWIFT;
  28. /**
  29. * Set the button `image` with an `url` and a placeholder.
  30. *
  31. * The download is asynchronous and cached.
  32. *
  33. * @param url The url for the image.
  34. * @param placeholder The image to be set initially, until the image request finishes.
  35. * @see sd_setImageWithURL:placeholderImage:options:
  36. */
  37. - (void)sd_setImageWithURL:(nullable NSURL *)url
  38. placeholderImage:(nullable UIImage *)placeholder NS_REFINED_FOR_SWIFT;
  39. /**
  40. * Set the button `image` with an `url`, placeholder and custom options.
  41. *
  42. * The download is asynchronous and cached.
  43. *
  44. * @param url The url for the image.
  45. * @param placeholder The image to be set initially, until the image request finishes.
  46. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  47. */
  48. - (void)sd_setImageWithURL:(nullable NSURL *)url
  49. placeholderImage:(nullable UIImage *)placeholder
  50. options:(SDWebImageOptions)options NS_REFINED_FOR_SWIFT;
  51. /**
  52. * Set the button `image` with an `url`, placeholder and custom options.
  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. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  59. * @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.
  60. */
  61. - (void)sd_setImageWithURL:(nullable NSURL *)url
  62. placeholderImage:(nullable UIImage *)placeholder
  63. options:(SDWebImageOptions)options
  64. context:(nullable SDWebImageContext *)context;
  65. /**
  66. * Set the button `image` with an `url`.
  67. *
  68. * The download is asynchronous and cached.
  69. *
  70. * @param url The url for the image.
  71. * @param completedBlock A block called when operation has been completed. This block has no return value
  72. * and takes the requested UIImage as first parameter. In case of error the image parameter
  73. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  74. * indicating if the image was retrieved from the local cache or from the network.
  75. * The fourth parameter is the original image url.
  76. */
  77. - (void)sd_setImageWithURL:(nullable NSURL *)url
  78. completed:(nullable SDExternalCompletionBlock)completedBlock;
  79. /**
  80. * Set the button `image` with an `url`, placeholder.
  81. *
  82. * The download is asynchronous and cached.
  83. *
  84. * @param url The url for the image.
  85. * @param placeholder The image to be set initially, until the image request finishes.
  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. placeholderImage:(nullable UIImage *)placeholder
  94. completed:(nullable SDExternalCompletionBlock)completedBlock NS_REFINED_FOR_SWIFT;
  95. /**
  96. * Set the button `image` with an `url`, placeholder and custom options.
  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 options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
  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. placeholderImage:(nullable UIImage *)placeholder
  111. options:(SDWebImageOptions)options
  112. completed:(nullable SDExternalCompletionBlock)completedBlock;
  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 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. * @note the progress block is executed on a background queue
  123. * @param completedBlock A block called when operation has been completed. This block has no return value
  124. * and takes the requested UIImage as first parameter. In case of error the image parameter
  125. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  126. * indicating if the image was retrieved from the local cache or from the network.
  127. * The fourth parameter is the original image url.
  128. */
  129. - (void)sd_setImageWithURL:(nullable NSURL *)url
  130. placeholderImage:(nullable UIImage *)placeholder
  131. options:(SDWebImageOptions)options
  132. progress:(nullable SDImageLoaderProgressBlock)progressBlock
  133. completed:(nullable SDExternalCompletionBlock)completedBlock;
  134. /**
  135. * Set the button `image` with an `url`, placeholder and custom options.
  136. *
  137. * The download is asynchronous and cached.
  138. *
  139. * @param url The url for the image.
  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 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.
  143. * @param progressBlock A block called while image is downloading
  144. * @note the progress block is executed on a background queue
  145. * @param completedBlock A block called when operation has been completed. This block has no return value
  146. * and takes the requested UIImage as first parameter. In case of error the image parameter
  147. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  148. * indicating if the image was retrieved from the local cache or from the network.
  149. * The fourth parameter is the original image url.
  150. */
  151. - (void)sd_setImageWithURL:(nullable NSURL *)url
  152. placeholderImage:(nullable UIImage *)placeholder
  153. options:(SDWebImageOptions)options
  154. context:(nullable SDWebImageContext *)context
  155. progress:(nullable SDImageLoaderProgressBlock)progressBlock
  156. completed:(nullable SDExternalCompletionBlock)completedBlock;
  157. #pragma mark - Alternate Image
  158. /**
  159. * Get the current alternateImage URL.
  160. */
  161. @property (nonatomic, strong, readonly, nullable) NSURL *sd_currentAlternateImageURL;
  162. /**
  163. * Set the button `alternateImage` with an `url`.
  164. *
  165. * The download is asynchronous and cached.
  166. *
  167. * @param url The url for the alternateImage.
  168. */
  169. - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url NS_REFINED_FOR_SWIFT;
  170. /**
  171. * Set the button `alternateImage` with an `url` and a placeholder.
  172. *
  173. * The download is asynchronous and cached.
  174. *
  175. * @param url The url for the alternateImage.
  176. * @param placeholder The alternateImage to be set initially, until the alternateImage request finishes.
  177. * @see sd_setAlternateImageWithURL:placeholderImage:options:
  178. */
  179. - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url
  180. placeholderImage:(nullable UIImage *)placeholder NS_REFINED_FOR_SWIFT;
  181. /**
  182. * Set the button `alternateImage` with an `url`, placeholder and custom options.
  183. *
  184. * The download is asynchronous and cached.
  185. *
  186. * @param url The url for the alternateImage.
  187. * @param placeholder The alternateImage to be set initially, until the alternateImage request finishes.
  188. * @param options The options to use when downloading the alternateImage. @see SDWebImageOptions for the possible values.
  189. */
  190. - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url
  191. placeholderImage:(nullable UIImage *)placeholder
  192. options:(SDWebImageOptions)options NS_REFINED_FOR_SWIFT;
  193. /**
  194. * Set the button `alternateImage` with an `url`, placeholder, custom options and context.
  195. *
  196. * The download is asynchronous and cached.
  197. *
  198. * @param url The url for the alternateImage.
  199. * @param placeholder The alternateImage to be set initially, until the alternateImage request finishes.
  200. * @param options The options to use when downloading the alternateImage. @see SDWebImageOptions for the possible values.
  201. * @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.
  202. */
  203. - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url
  204. placeholderImage:(nullable UIImage *)placeholder
  205. options:(SDWebImageOptions)options
  206. context:(nullable SDWebImageContext *)context;
  207. /**
  208. * Set the button `alternateImage` with an `url`.
  209. *
  210. * The download is asynchronous and cached.
  211. *
  212. * @param url The url for the alternateImage.
  213. * @param completedBlock A block called when operation has been completed. This block has no return value
  214. * and takes the requested UIImage as first parameter. In case of error the alternateImage parameter
  215. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  216. * indicating if the alternateImage was retrieved from the local cache or from the network.
  217. * The fourth parameter is the original alternateImage url.
  218. */
  219. - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url
  220. completed:(nullable SDExternalCompletionBlock)completedBlock;
  221. /**
  222. * Set the button `alternateImage` with an `url`, placeholder.
  223. *
  224. * The download is asynchronous and cached.
  225. *
  226. * @param url The url for the alternateImage.
  227. * @param placeholder The alternateImage to be set initially, until the alternateImage request finishes.
  228. * @param completedBlock A block called when operation has been completed. This block has no return value
  229. * and takes the requested UIImage as first parameter. In case of error the alternateImage parameter
  230. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  231. * indicating if the alternateImage was retrieved from the local cache or from the network.
  232. * The fourth parameter is the original alternateImage url.
  233. */
  234. - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url
  235. placeholderImage:(nullable UIImage *)placeholder
  236. completed:(nullable SDExternalCompletionBlock)completedBlock NS_REFINED_FOR_SWIFT;
  237. /**
  238. * Set the button `alternateImage` with an `url`, placeholder and custom options.
  239. *
  240. * The download is asynchronous and cached.
  241. *
  242. * @param url The url for the alternateImage.
  243. * @param placeholder The alternateImage to be set initially, until the alternateImage request finishes.
  244. * @param options The options to use when downloading the alternateImage. @see SDWebImageOptions for the possible values.
  245. * @param completedBlock A block called when operation has been completed. This block has no return value
  246. * and takes the requested UIImage as first parameter. In case of error the alternateImage parameter
  247. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  248. * indicating if the alternateImage was retrieved from the local cache or from the network.
  249. * The fourth parameter is the original alternateImage url.
  250. */
  251. - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url
  252. placeholderImage:(nullable UIImage *)placeholder
  253. options:(SDWebImageOptions)options
  254. completed:(nullable SDExternalCompletionBlock)completedBlock;
  255. /**
  256. * Set the button `alternateImage` with an `url`, placeholder and custom options.
  257. *
  258. * The download is asynchronous and cached.
  259. *
  260. * @param url The url for the alternateImage.
  261. * @param placeholder The alternateImage to be set initially, until the alternateImage request finishes.
  262. * @param options The options to use when downloading the alternateImage. @see SDWebImageOptions for the possible values.
  263. * @param progressBlock A block called while alternateImage is downloading
  264. * @note the progress block is executed on a background queue
  265. * @param completedBlock A block called when operation has been completed. This block has no return value
  266. * and takes the requested UIImage as first parameter. In case of error the alternateImage parameter
  267. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  268. * indicating if the alternateImage was retrieved from the local cache or from the network.
  269. * The fourth parameter is the original alternateImage url.
  270. */
  271. - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url
  272. placeholderImage:(nullable UIImage *)placeholder
  273. options:(SDWebImageOptions)options
  274. progress:(nullable SDImageLoaderProgressBlock)progressBlock
  275. completed:(nullable SDExternalCompletionBlock)completedBlock;
  276. /**
  277. * Set the button `alternateImage` with an `url`, placeholder, custom options and context.
  278. *
  279. * The download is asynchronous and cached.
  280. *
  281. * @param url The url for the alternateImage.
  282. * @param placeholder The alternateImage to be set initially, until the alternateImage request finishes.
  283. * @param options The options to use when downloading the alternateImage. @see SDWebImageOptions for the possible values.
  284. * @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.
  285. * @param progressBlock A block called while alternateImage is downloading
  286. * @note the progress block is executed on a background queue
  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 alternateImage parameter
  289. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  290. * indicating if the alternateImage was retrieved from the local cache or from the network.
  291. * The fourth parameter is the original alternateImage url.
  292. */
  293. - (void)sd_setAlternateImageWithURL:(nullable NSURL *)url
  294. placeholderImage:(nullable UIImage *)placeholder
  295. options:(SDWebImageOptions)options
  296. context:(nullable SDWebImageContext *)context
  297. progress:(nullable SDImageLoaderProgressBlock)progressBlock
  298. completed:(nullable SDExternalCompletionBlock)completedBlock;
  299. #pragma mark - Cancel
  300. /**
  301. * Cancel the current image download
  302. */
  303. - (void)sd_cancelCurrentImageLoad;
  304. /**
  305. * Cancel the current alternateImage download
  306. */
  307. - (void)sd_cancelCurrentAlternateImageLoad;
  308. @end
  309. #endif