UIButton+Kingfisher.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. //
  2. // UIButton+Kingfisher.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 15/4/13.
  6. //
  7. // Copyright (c) 2016 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. #if os(iOS) || os(tvOS)
  27. import UIKit
  28. /**
  29. * Set image to use from web for a specified state.
  30. */
  31. extension UIButton {
  32. /**
  33. Set an image to use for a specified state with a resource.
  34. It will ask for Kingfisher's manager to get the image for the `cacheKey` property in `resource` and then set it for a button state.
  35. The memory and disk will be searched first. If the manager does not find it, it will try to download the image at the `resource.downloadURL` and store it with `resource.cacheKey` for next use.
  36. - parameter resource: Resource object contains information such as `cacheKey` and `downloadURL`.
  37. - parameter state: The state that uses the specified image.
  38. - returns: A task represents the retrieving process.
  39. */
  40. public func kf_setImageWithResource(resource: Resource,
  41. forState state: UIControlState) -> RetrieveImageTask
  42. {
  43. return kf_setImageWithResource(resource, forState: state, placeholderImage: nil, optionsInfo: nil, progressBlock: nil, completionHandler: nil)
  44. }
  45. /**
  46. Set an image to use for a specified state with a URL.
  47. It will ask for Kingfisher's manager to get the image for the URL and then set it for a button state.
  48. The memory and disk will be searched first with `URL.absoluteString` as the cache key. If the manager does not find it, it will try to download the image at this URL and store the image with `URL.absoluteString` as cache key for next use.
  49. If you need to specify the key other than `URL.absoluteString`, please use resource version of these APIs with `resource.cacheKey` set to what you want.
  50. - parameter URL: The URL of image for specified state.
  51. - parameter state: The state that uses the specified image.
  52. - returns: A task represents the retrieving process.
  53. */
  54. public func kf_setImageWithURL(URL: NSURL,
  55. forState state: UIControlState) -> RetrieveImageTask
  56. {
  57. return kf_setImageWithURL(URL, forState: state, placeholderImage: nil, optionsInfo: nil, progressBlock: nil, completionHandler: nil)
  58. }
  59. /**
  60. Set an image to use for a specified state with a resource and a placeholder image.
  61. - parameter resource: Resource object contains information such as `cacheKey` and `downloadURL`.
  62. - parameter state: The state that uses the specified image.
  63. - parameter placeholderImage: A placeholder image when retrieving the image at URL.
  64. - returns: A task represents the retrieving process.
  65. */
  66. public func kf_setImageWithResource(resource: Resource,
  67. forState state: UIControlState,
  68. placeholderImage: UIImage?) -> RetrieveImageTask
  69. {
  70. return kf_setImageWithResource(resource, forState: state, placeholderImage: placeholderImage, optionsInfo: nil, progressBlock: nil, completionHandler: nil)
  71. }
  72. /**
  73. Set an image to use for a specified state with a URL and a placeholder image.
  74. - parameter URL: The URL of image for specified state.
  75. - parameter state: The state that uses the specified image.
  76. - parameter placeholderImage: A placeholder image when retrieving the image at URL.
  77. - returns: A task represents the retrieving process.
  78. */
  79. public func kf_setImageWithURL(URL: NSURL,
  80. forState state: UIControlState,
  81. placeholderImage: UIImage?) -> RetrieveImageTask
  82. {
  83. return kf_setImageWithURL(URL, forState: state, placeholderImage: placeholderImage, optionsInfo: nil, progressBlock: nil, completionHandler: nil)
  84. }
  85. /**
  86. Set an image to use for a specified state with a resource, a placeholder image and options.
  87. - parameter resource: Resource object contains information such as `cacheKey` and `downloadURL`.
  88. - parameter state: The state that uses the specified image.
  89. - parameter placeholderImage: A placeholder image when retrieving the image at URL.
  90. - parameter optionsInfo: A dictionary could control some behaviors. See `KingfisherOptionsInfo` for more.
  91. - returns: A task represents the retrieving process.
  92. */
  93. public func kf_setImageWithResource(resource: Resource,
  94. forState state: UIControlState,
  95. placeholderImage: UIImage?,
  96. optionsInfo: KingfisherOptionsInfo?) -> RetrieveImageTask
  97. {
  98. return kf_setImageWithResource(resource, forState: state, placeholderImage: placeholderImage, optionsInfo: optionsInfo, progressBlock: nil, completionHandler: nil)
  99. }
  100. /**
  101. Set an image to use for a specified state with a URL, a placeholder image and options.
  102. - parameter URL: The URL of image for specified state.
  103. - parameter state: The state that uses the specified image.
  104. - parameter placeholderImage: A placeholder image when retrieving the image at URL.
  105. - parameter optionsInfo: A dictionary could control some behaviors. See `KingfisherOptionsInfo` for more.
  106. - returns: A task represents the retrieving process.
  107. */
  108. public func kf_setImageWithURL(URL: NSURL,
  109. forState state: UIControlState,
  110. placeholderImage: UIImage?,
  111. optionsInfo: KingfisherOptionsInfo?) -> RetrieveImageTask
  112. {
  113. return kf_setImageWithURL(URL, forState: state, placeholderImage: placeholderImage, optionsInfo: optionsInfo, progressBlock: nil, completionHandler: nil)
  114. }
  115. /**
  116. Set an image to use for a specified state with a resource, a placeholder image, options and completion handler.
  117. - parameter resource: Resource object contains information such as `cacheKey` and `downloadURL`.
  118. - parameter state: The state that uses the specified image.
  119. - parameter placeholderImage: A placeholder image when retrieving the image at URL.
  120. - parameter optionsInfo: A dictionary could control some behaviors. See `KingfisherOptionsInfo` for more.
  121. - parameter completionHandler: Called when the image retrieved and set.
  122. - returns: A task represents the retrieving process.
  123. - note: `completionHandler` will be invoked in main thread.
  124. The `CallbackDispatchQueue` specified in `optionsInfo` will not be used in callbacks of this method.
  125. */
  126. public func kf_setImageWithResource(resource: Resource,
  127. forState state: UIControlState,
  128. placeholderImage: UIImage?,
  129. optionsInfo: KingfisherOptionsInfo?,
  130. completionHandler: CompletionHandler?) -> RetrieveImageTask
  131. {
  132. return kf_setImageWithResource(resource, forState: state, placeholderImage: placeholderImage, optionsInfo: optionsInfo, progressBlock: nil, completionHandler: completionHandler)
  133. }
  134. /**
  135. Set an image to use for a specified state with a URL, a placeholder image, options and completion handler.
  136. - parameter URL: The URL of image for specified state.
  137. - parameter state: The state that uses the specified image.
  138. - parameter placeholderImage: A placeholder image when retrieving the image at URL.
  139. - parameter optionsInfo: A dictionary could control some behaviors. See `KingfisherOptionsInfo` for more.
  140. - parameter completionHandler: Called when the image retrieved and set.
  141. - returns: A task represents the retrieving process.
  142. - note: `completionHandler` will be invoked in main thread.
  143. The `CallbackDispatchQueue` specified in `optionsInfo` will not be used in callbacks of this method.
  144. */
  145. public func kf_setImageWithURL(URL: NSURL,
  146. forState state: UIControlState,
  147. placeholderImage: UIImage?,
  148. optionsInfo: KingfisherOptionsInfo?,
  149. completionHandler: CompletionHandler?) -> RetrieveImageTask
  150. {
  151. return kf_setImageWithURL(URL, forState: state, placeholderImage: placeholderImage, optionsInfo: optionsInfo, progressBlock: nil, completionHandler: completionHandler)
  152. }
  153. /**
  154. Set an image to use for a specified state with a resource, a placeholder image, options, progress handler and completion handler.
  155. - parameter resource: Resource object contains information such as `cacheKey` and `downloadURL`.
  156. - parameter state: The state that uses the specified image.
  157. - parameter placeholderImage: A placeholder image when retrieving the image at URL.
  158. - parameter optionsInfo: A dictionary could control some behaviors. See `KingfisherOptionsInfo` for more.
  159. - parameter progressBlock: Called when the image downloading progress gets updated.
  160. - parameter completionHandler: Called when the image retrieved and set.
  161. - returns: A task represents the retrieving process.
  162. - note: Both the `progressBlock` and `completionHandler` will be invoked in main thread.
  163. The `CallbackDispatchQueue` specified in `optionsInfo` will not be used in callbacks of this method.
  164. */
  165. public func kf_setImageWithResource(resource: Resource,
  166. forState state: UIControlState,
  167. placeholderImage: UIImage?,
  168. optionsInfo: KingfisherOptionsInfo?,
  169. progressBlock: DownloadProgressBlock?,
  170. completionHandler: CompletionHandler?) -> RetrieveImageTask
  171. {
  172. setImage(placeholderImage, forState: state)
  173. kf_setWebURL(resource.downloadURL, forState: state)
  174. let task = KingfisherManager.sharedManager.retrieveImageWithResource(resource, optionsInfo: optionsInfo,
  175. progressBlock: { receivedSize, totalSize in
  176. if let progressBlock = progressBlock {
  177. progressBlock(receivedSize: receivedSize, totalSize: totalSize)
  178. }
  179. },
  180. completionHandler: {[weak self] image, error, cacheType, imageURL in
  181. dispatch_async_safely_to_main_queue {
  182. if let sSelf = self {
  183. sSelf.kf_setImageTask(nil)
  184. if imageURL == sSelf.kf_webURLForState(state) && image != nil {
  185. sSelf.setImage(image, forState: state)
  186. }
  187. completionHandler?(image: image, error: error, cacheType: cacheType, imageURL: imageURL)
  188. }
  189. }
  190. })
  191. kf_setImageTask(task)
  192. return task
  193. }
  194. /**
  195. Set an image to use for a specified state with a URL, a placeholder image, options, progress handler and completion handler.
  196. - parameter URL: The URL of image for specified state.
  197. - parameter state: The state that uses the specified image.
  198. - parameter placeholderImage: A placeholder image when retrieving the image at URL.
  199. - parameter optionsInfo: A dictionary could control some behaviors. See `KingfisherOptionsInfo` for more.
  200. - parameter progressBlock: Called when the image downloading progress gets updated.
  201. - parameter completionHandler: Called when the image retrieved and set.
  202. - returns: A task represents the retrieving process.
  203. - note: Both the `progressBlock` and `completionHandler` will be invoked in main thread.
  204. The `CallbackDispatchQueue` specified in `optionsInfo` will not be used in callbacks of this method.
  205. */
  206. public func kf_setImageWithURL(URL: NSURL,
  207. forState state: UIControlState,
  208. placeholderImage: UIImage?,
  209. optionsInfo: KingfisherOptionsInfo?,
  210. progressBlock: DownloadProgressBlock?,
  211. completionHandler: CompletionHandler?) -> RetrieveImageTask
  212. {
  213. return kf_setImageWithResource(Resource(downloadURL: URL),
  214. forState: state,
  215. placeholderImage: placeholderImage,
  216. optionsInfo: optionsInfo,
  217. progressBlock: progressBlock,
  218. completionHandler: completionHandler)
  219. }
  220. }
  221. private var lastURLKey: Void?
  222. private var imageTaskKey: Void?
  223. // MARK: - Runtime for UIButton image
  224. extension UIButton {
  225. /**
  226. Get the image URL binded to this button for a specified state.
  227. - parameter state: The state that uses the specified image.
  228. - returns: Current URL for image.
  229. */
  230. public func kf_webURLForState(state: UIControlState) -> NSURL? {
  231. return kf_webURLs[NSNumber(unsignedLong:state.rawValue)] as? NSURL
  232. }
  233. private func kf_setWebURL(URL: NSURL, forState state: UIControlState) {
  234. kf_webURLs[NSNumber(unsignedLong:state.rawValue)] = URL
  235. }
  236. private var kf_webURLs: NSMutableDictionary {
  237. var dictionary = objc_getAssociatedObject(self, &lastURLKey) as? NSMutableDictionary
  238. if dictionary == nil {
  239. dictionary = NSMutableDictionary()
  240. kf_setWebURLs(dictionary!)
  241. }
  242. return dictionary!
  243. }
  244. private func kf_setWebURLs(URLs: NSMutableDictionary) {
  245. objc_setAssociatedObject(self, &lastURLKey, URLs, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  246. }
  247. private var kf_imageTask: RetrieveImageTask? {
  248. return objc_getAssociatedObject(self, &imageTaskKey) as? RetrieveImageTask
  249. }
  250. private func kf_setImageTask(task: RetrieveImageTask?) {
  251. objc_setAssociatedObject(self, &imageTaskKey, task, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  252. }
  253. }
  254. /**
  255. * Set background image to use from web for a specified state.
  256. */
  257. extension UIButton {
  258. /**
  259. Set the background image to use for a specified state with a resource.
  260. It will ask for Kingfisher's manager to get the image for the `cacheKey` property in `resource` and then set it for a button state.
  261. The memory and disk will be searched first. If the manager does not find it, it will try to download the image at the `resource.downloadURL` and store it with `resource.cacheKey` for next use.
  262. - parameter resource: Resource object contains information such as `cacheKey` and `downloadURL`.
  263. - parameter state: The state that uses the specified image.
  264. - returns: A task represents the retrieving process.
  265. */
  266. public func kf_setBackgroundImageWithResource(resource: Resource,
  267. forState state: UIControlState) -> RetrieveImageTask
  268. {
  269. return kf_setBackgroundImageWithResource(resource, forState: state, placeholderImage: nil, optionsInfo: nil, progressBlock: nil, completionHandler: nil)
  270. }
  271. /**
  272. Set the background image to use for a specified state with a URL.
  273. It will ask for Kingfisher's manager to get the image for the URL and then set it for a button state.
  274. The memory and disk will be searched first with `URL.absoluteString` as the cache key. If the manager does not find it, it will try to download the image at this URL and store the image with `URL.absoluteString` as cache key for next use.
  275. If you need to specify the key other than `URL.absoluteString`, please use resource version of these APIs with `resource.cacheKey` set to what you want.
  276. - parameter URL: The URL of image for specified state.
  277. - parameter state: The state that uses the specified image.
  278. - returns: A task represents the retrieving process.
  279. */
  280. public func kf_setBackgroundImageWithURL(URL: NSURL,
  281. forState state: UIControlState) -> RetrieveImageTask
  282. {
  283. return kf_setBackgroundImageWithURL(URL, forState: state, placeholderImage: nil, optionsInfo: nil, progressBlock: nil, completionHandler: nil)
  284. }
  285. /**
  286. Set the background image to use for a specified state with a resource and a placeholder image.
  287. - parameter resource: Resource object contains information such as `cacheKey` and `downloadURL`.
  288. - parameter state: The state that uses the specified image.
  289. - parameter placeholderImage: A placeholder image when retrieving the image at URL.
  290. - returns: A task represents the retrieving process.
  291. */
  292. public func kf_setBackgroundImageWithResource(resource: Resource,
  293. forState state: UIControlState,
  294. placeholderImage: UIImage?) -> RetrieveImageTask
  295. {
  296. return kf_setBackgroundImageWithResource(resource, forState: state, placeholderImage: placeholderImage, optionsInfo: nil, progressBlock: nil, completionHandler: nil)
  297. }
  298. /**
  299. Set the background image to use for a specified state with a URL and a placeholder image.
  300. - parameter URL: The URL of image for specified state.
  301. - parameter state: The state that uses the specified image.
  302. - parameter placeholderImage: A placeholder image when retrieving the image at URL.
  303. - returns: A task represents the retrieving process.
  304. */
  305. public func kf_setBackgroundImageWithURL(URL: NSURL,
  306. forState state: UIControlState,
  307. placeholderImage: UIImage?) -> RetrieveImageTask
  308. {
  309. return kf_setBackgroundImageWithURL(URL, forState: state, placeholderImage: placeholderImage, optionsInfo: nil, progressBlock: nil, completionHandler: nil)
  310. }
  311. /**
  312. Set the background image to use for a specified state with a resource, a placeholder image and options.
  313. - parameter resource: Resource object contains information such as `cacheKey` and `downloadURL`.
  314. - parameter state: The state that uses the specified image.
  315. - parameter placeholderImage: A placeholder image when retrieving the image at URL.
  316. - parameter optionsInfo: A dictionary could control some behaviors. See `KingfisherOptionsInfo` for more.
  317. - returns: A task represents the retrieving process.
  318. */
  319. public func kf_setBackgroundImageWithResource(resource: Resource,
  320. forState state: UIControlState,
  321. placeholderImage: UIImage?,
  322. optionsInfo: KingfisherOptionsInfo?) -> RetrieveImageTask
  323. {
  324. return kf_setBackgroundImageWithResource(resource, forState: state, placeholderImage: placeholderImage, optionsInfo: optionsInfo, progressBlock: nil, completionHandler: nil)
  325. }
  326. /**
  327. Set the background image to use for a specified state with a URL, a placeholder image and options.
  328. - parameter URL: The URL of image for specified state.
  329. - parameter state: The state that uses the specified image.
  330. - parameter placeholderImage: A placeholder image when retrieving the image at URL.
  331. - parameter optionsInfo: A dictionary could control some behaviors. See `KingfisherOptionsInfo` for more.
  332. - returns: A task represents the retrieving process.
  333. */
  334. public func kf_setBackgroundImageWithURL(URL: NSURL,
  335. forState state: UIControlState,
  336. placeholderImage: UIImage?,
  337. optionsInfo: KingfisherOptionsInfo?) -> RetrieveImageTask
  338. {
  339. return kf_setBackgroundImageWithURL(URL, forState: state, placeholderImage: placeholderImage, optionsInfo: optionsInfo, progressBlock: nil, completionHandler: nil)
  340. }
  341. /**
  342. Set the background image to use for a specified state with a resource, a placeholder image, options and completion handler.
  343. - parameter resource: Resource object contains information such as `cacheKey` and `downloadURL`.
  344. - parameter state: The state that uses the specified image.
  345. - parameter placeholderImage: A placeholder image when retrieving the image at URL.
  346. - parameter optionsInfo: A dictionary could control some behaviors. See `KingfisherOptionsInfo` for more.
  347. - parameter completionHandler: Called when the image retrieved and set.
  348. - returns: A task represents the retrieving process.
  349. - note: `completionHandler` will be invoked in main thread.
  350. The `CallbackDispatchQueue` specified in `optionsInfo` will not be used in callbacks of this method.
  351. */
  352. public func kf_setBackgroundImageWithResource(resource: Resource,
  353. forState state: UIControlState,
  354. placeholderImage: UIImage?,
  355. optionsInfo: KingfisherOptionsInfo?,
  356. completionHandler: CompletionHandler?) -> RetrieveImageTask
  357. {
  358. return kf_setBackgroundImageWithResource(resource, forState: state, placeholderImage: placeholderImage, optionsInfo: optionsInfo, progressBlock: nil, completionHandler: completionHandler)
  359. }
  360. /**
  361. Set the background image to use for a specified state with a URL, a placeholder image, options and completion handler.
  362. - parameter URL: The URL of image for specified state.
  363. - parameter state: The state that uses the specified image.
  364. - parameter placeholderImage: A placeholder image when retrieving the image at URL.
  365. - parameter optionsInfo: A dictionary could control some behaviors. See `KingfisherOptionsInfo` for more.
  366. - parameter completionHandler: Called when the image retrieved and set.
  367. - returns: A task represents the retrieving process.
  368. - note: `completionHandler` will be invoked in main thread.
  369. The `CallbackDispatchQueue` specified in `optionsInfo` will not be used in callbacks of this method.
  370. */
  371. public func kf_setBackgroundImageWithURL(URL: NSURL,
  372. forState state: UIControlState,
  373. placeholderImage: UIImage?,
  374. optionsInfo: KingfisherOptionsInfo?,
  375. completionHandler: CompletionHandler?) -> RetrieveImageTask
  376. {
  377. return kf_setBackgroundImageWithURL(URL, forState: state, placeholderImage: placeholderImage, optionsInfo: optionsInfo, progressBlock: nil, completionHandler: completionHandler)
  378. }
  379. /**
  380. Set the background image to use for a specified state with a resource,
  381. a placeholder image, options progress handler and completion handler.
  382. - parameter resource: Resource object contains information such as `cacheKey` and `downloadURL`.
  383. - parameter state: The state that uses the specified image.
  384. - parameter placeholderImage: A placeholder image when retrieving the image at URL.
  385. - parameter optionsInfo: A dictionary could control some behaviors. See `KingfisherOptionsInfo` for more.
  386. - parameter progressBlock: Called when the image downloading progress gets updated.
  387. - parameter completionHandler: Called when the image retrieved and set.
  388. - returns: A task represents the retrieving process.
  389. - note: Both the `progressBlock` and `completionHandler` will be invoked in main thread.
  390. The `CallbackDispatchQueue` specified in `optionsInfo` will not be used in callbacks of this method.
  391. */
  392. public func kf_setBackgroundImageWithResource(resource: Resource,
  393. forState state: UIControlState,
  394. placeholderImage: UIImage?,
  395. optionsInfo: KingfisherOptionsInfo?,
  396. progressBlock: DownloadProgressBlock?,
  397. completionHandler: CompletionHandler?) -> RetrieveImageTask
  398. {
  399. setBackgroundImage(placeholderImage, forState: state)
  400. kf_setBackgroundWebURL(resource.downloadURL, forState: state)
  401. let task = KingfisherManager.sharedManager.retrieveImageWithResource(resource, optionsInfo: optionsInfo,
  402. progressBlock: { receivedSize, totalSize in
  403. if let progressBlock = progressBlock {
  404. progressBlock(receivedSize: receivedSize, totalSize: totalSize)
  405. }
  406. },
  407. completionHandler: { [weak self] image, error, cacheType, imageURL in
  408. dispatch_async_safely_to_main_queue {
  409. if let sSelf = self {
  410. sSelf.kf_setBackgroundImageTask(nil)
  411. if imageURL == sSelf.kf_backgroundWebURLForState(state) && image != nil {
  412. sSelf.setBackgroundImage(image, forState: state)
  413. }
  414. completionHandler?(image: image, error: error, cacheType: cacheType, imageURL: imageURL)
  415. }
  416. }
  417. })
  418. kf_setBackgroundImageTask(task)
  419. return task
  420. }
  421. /**
  422. Set the background image to use for a specified state with a URL,
  423. a placeholder image, options progress handler and completion handler.
  424. - parameter URL: The URL of image for specified state.
  425. - parameter state: The state that uses the specified image.
  426. - parameter placeholderImage: A placeholder image when retrieving the image at URL.
  427. - parameter optionsInfo: A dictionary could control some behaviors. See `KingfisherOptionsInfo` for more.
  428. - parameter progressBlock: Called when the image downloading progress gets updated.
  429. - parameter completionHandler: Called when the image retrieved and set.
  430. - returns: A task represents the retrieving process.
  431. - note: Both the `progressBlock` and `completionHandler` will be invoked in main thread.
  432. The `CallbackDispatchQueue` specified in `optionsInfo` will not be used in callbacks of this method.
  433. */
  434. public func kf_setBackgroundImageWithURL(URL: NSURL,
  435. forState state: UIControlState,
  436. placeholderImage: UIImage?,
  437. optionsInfo: KingfisherOptionsInfo?,
  438. progressBlock: DownloadProgressBlock?,
  439. completionHandler: CompletionHandler?) -> RetrieveImageTask
  440. {
  441. return kf_setBackgroundImageWithResource(Resource(downloadURL: URL),
  442. forState: state,
  443. placeholderImage: placeholderImage,
  444. optionsInfo: optionsInfo,
  445. progressBlock: progressBlock,
  446. completionHandler: completionHandler)
  447. }
  448. }
  449. private var lastBackgroundURLKey: Void?
  450. private var backgroundImageTaskKey: Void?
  451. // MARK: - Runtime for UIButton background image
  452. extension UIButton {
  453. /**
  454. Get the background image URL binded to this button for a specified state.
  455. - parameter state: The state that uses the specified background image.
  456. - returns: Current URL for background image.
  457. */
  458. public func kf_backgroundWebURLForState(state: UIControlState) -> NSURL? {
  459. return kf_backgroundWebURLs[NSNumber(unsignedLong:state.rawValue)] as? NSURL
  460. }
  461. private func kf_setBackgroundWebURL(URL: NSURL, forState state: UIControlState) {
  462. kf_backgroundWebURLs[NSNumber(unsignedLong:state.rawValue)] = URL
  463. }
  464. private var kf_backgroundWebURLs: NSMutableDictionary {
  465. var dictionary = objc_getAssociatedObject(self, &lastBackgroundURLKey) as? NSMutableDictionary
  466. if dictionary == nil {
  467. dictionary = NSMutableDictionary()
  468. kf_setBackgroundWebURLs(dictionary!)
  469. }
  470. return dictionary!
  471. }
  472. private func kf_setBackgroundWebURLs(URLs: NSMutableDictionary) {
  473. objc_setAssociatedObject(self, &lastBackgroundURLKey, URLs, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  474. }
  475. private var kf_backgroundImageTask: RetrieveImageTask? {
  476. return objc_getAssociatedObject(self, &backgroundImageTaskKey) as? RetrieveImageTask
  477. }
  478. private func kf_setBackgroundImageTask(task: RetrieveImageTask?) {
  479. objc_setAssociatedObject(self, &backgroundImageTaskKey, task, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  480. }
  481. }
  482. // MARK: - Cancel image download tasks.
  483. extension UIButton {
  484. /**
  485. Cancel the image download task bounded to the image view if it is running.
  486. Nothing will happen if the downloading has already finished.
  487. */
  488. public func kf_cancelImageDownloadTask() {
  489. kf_imageTask?.downloadTask?.cancel()
  490. }
  491. /**
  492. Cancel the background image download task bounded to the image view if it is running.
  493. Nothing will happen if the downloading has already finished.
  494. */
  495. public func kf_cancelBackgroundImageDownloadTask() {
  496. kf_backgroundImageTask?.downloadTask?.cancel()
  497. }
  498. }
  499. #elseif os(OSX)
  500. import AppKit
  501. extension NSButton {
  502. // Not Implemented yet.
  503. }
  504. #endif