WKInterfaceImage+Kingfisher.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // WKInterfaceImage+Kingfisher.swift
  3. // Kingfisher
  4. //
  5. // Created by Rodrigo Borges Soares on 04/05/18.
  6. //
  7. // Copyright (c) 2018 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. import WatchKit
  27. // MARK: - Extension methods.
  28. /**
  29. * Set image to use from web.
  30. */
  31. extension KingfisherClass where Base: WKInterfaceImage {
  32. /**
  33. Set an image with a resource.
  34. - parameter resource: Resource object contains information such as `cacheKey` and `downloadURL`.
  35. - parameter options: A dictionary could control some behaviors. See `KingfisherOptionsInfo` for more.
  36. - parameter progressBlock: Called when the image downloading progress gets updated.
  37. - parameter completionHandler: Called when the image retrieved and set.
  38. - returns: A task represents the retrieving process.
  39. */
  40. @discardableResult
  41. public func setImage(_ resource: Resource?,
  42. options: KingfisherOptionsInfo? = nil,
  43. progressBlock: DownloadProgressBlock? = nil,
  44. completionHandler: CompletionHandler? = nil) -> RetrieveImageTask {
  45. guard let resource = resource else {
  46. setWebURL(nil)
  47. completionHandler?(nil, nil, .none, nil)
  48. return .empty
  49. }
  50. setWebURL(resource.downloadURL)
  51. let task = KingfisherManager.shared.retrieveImage(
  52. with: resource,
  53. options: KingfisherManager.shared.defaultOptions + (options ?? .empty),
  54. progressBlock: { receivedSize, totalSize in
  55. guard resource.downloadURL == self.webURL else {
  56. return
  57. }
  58. progressBlock?(receivedSize, totalSize)
  59. },
  60. completionHandler: { [weak base] image, error, cacheType, imageURL in
  61. DispatchQueue.main.safeAsync {
  62. guard let strongBase = base, imageURL == self.webURL else {
  63. completionHandler?(image, error, cacheType, imageURL)
  64. return
  65. }
  66. self.setImageTask(nil)
  67. guard let image = image else {
  68. completionHandler?(nil, error, cacheType, imageURL)
  69. return
  70. }
  71. strongBase.setImage(image)
  72. completionHandler?(image, error, cacheType, imageURL)
  73. }
  74. })
  75. setImageTask(task)
  76. return task
  77. }
  78. /**
  79. Cancel the image download task bounded to the image view if it is running.
  80. Nothing will happen if the downloading has already finished.
  81. */
  82. public func cancelDownloadTask() {
  83. imageTask?.cancel()
  84. }
  85. }
  86. // MARK: - Associated Object
  87. private var lastURLKey: Void?
  88. private var imageTaskKey: Void?
  89. extension KingfisherClass where Base: WKInterfaceImage {
  90. /// Get the image URL binded to this image view.
  91. public var webURL: URL? {
  92. return objc_getAssociatedObject(base, &lastURLKey) as? URL
  93. }
  94. fileprivate func setWebURL(_ url: URL?) {
  95. objc_setAssociatedObject(base, &lastURLKey, url, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  96. }
  97. fileprivate var imageTask: RetrieveImageTask? {
  98. return objc_getAssociatedObject(base, &imageTaskKey) as? RetrieveImageTask
  99. }
  100. fileprivate func setImageTask(_ task: RetrieveImageTask?) {
  101. objc_setAssociatedObject(base, &imageTaskKey, task, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  102. }
  103. }