Kingfisher.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // Kingfisher.swift
  3. // Kingfisher
  4. //
  5. // Created by Wei Wang on 16/9/14.
  6. //
  7. // Copyright (c) 2019 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 Foundation
  27. import ImageIO
  28. #if os(macOS)
  29. import AppKit
  30. public typealias KFCrossPlatformImage = NSImage
  31. public typealias KFCrossPlatformView = NSView
  32. public typealias KFCrossPlatformColor = NSColor
  33. public typealias KFCrossPlatformImageView = NSImageView
  34. public typealias KFCrossPlatformButton = NSButton
  35. // `NSImage` is not yet Sendable. We have to assume it sendable to resolve warnings in Kingfisher.
  36. #if compiler(>=6)
  37. extension KFCrossPlatformImage: @retroactive @unchecked Sendable { }
  38. #else
  39. extension KFCrossPlatformImage: @unchecked Sendable { }
  40. #endif // compiler(>=6)
  41. #else // os(macOS)
  42. import UIKit
  43. public typealias KFCrossPlatformImage = UIImage
  44. public typealias KFCrossPlatformColor = UIColor
  45. #if !os(watchOS)
  46. public typealias KFCrossPlatformImageView = UIImageView
  47. public typealias KFCrossPlatformView = UIView
  48. public typealias KFCrossPlatformButton = UIButton
  49. #if canImport(TVUIKit)
  50. import TVUIKit
  51. #endif // canImport(TVUIKit)
  52. #if canImport(CarPlay) && !targetEnvironment(macCatalyst)
  53. import CarPlay
  54. #endif // canImport(CarPlay) && !targetEnvironment(macCatalyst)
  55. #else // !os(watchOS)
  56. import WatchKit
  57. #endif // !os(watchOS)
  58. #endif // os(macOS)
  59. /// Wrapper for Kingfisher compatible types. This type provides an extension point for
  60. /// convenience methods in Kingfisher.
  61. public struct KingfisherWrapper<Base>: @unchecked Sendable {
  62. public let base: Base
  63. public init(_ base: Base) {
  64. self.base = base
  65. }
  66. }
  67. /// Represents an object type that is compatible with Kingfisher. You can use ``kf`` property to get a
  68. /// value in the namespace of Kingfisher.
  69. ///
  70. /// In Kingfisher, most of related classes that contains an image (such as `UIImage`, `UIButton`, `NSImageView` and
  71. /// more) conform to this protocol, and provides the helper methods for setting an image easily. You can access the `kf`
  72. /// property and call its `setImage` method with a certain URL:
  73. ///
  74. /// ```swift
  75. /// let imageView: UIImageView
  76. /// let url = URL(string: "https://example.com/image.jpg")
  77. /// imageView.kf.setImage(with: url)
  78. /// ```
  79. ///
  80. /// For more about basic usage of Kingfisher, check the <doc:CommonTasks> documentation.
  81. public protocol KingfisherCompatible: AnyObject { }
  82. /// Represents a value type that is compatible with Kingfisher. You can use ``kf`` property to get a
  83. /// value in the namespace of Kingfisher.
  84. public protocol KingfisherCompatibleValue {}
  85. extension KingfisherCompatible {
  86. /// Gets a namespace holder for Kingfisher compatible types.
  87. public var kf: KingfisherWrapper<Self> {
  88. get { return KingfisherWrapper(self) }
  89. set { }
  90. }
  91. }
  92. extension KingfisherCompatibleValue {
  93. /// Gets a namespace holder for Kingfisher compatible types.
  94. public var kf: KingfisherWrapper<Self> {
  95. get { return KingfisherWrapper(self) }
  96. set { }
  97. }
  98. }
  99. extension KFCrossPlatformImage : KingfisherCompatible { }
  100. #if !os(watchOS)
  101. extension KFCrossPlatformImageView : KingfisherCompatible { }
  102. extension KFCrossPlatformButton : KingfisherCompatible { }
  103. extension NSTextAttachment : KingfisherCompatible { }
  104. #else
  105. extension WKInterfaceImage : KingfisherCompatible { }
  106. #endif
  107. #if canImport(PhotosUI)
  108. import PhotosUI
  109. extension PHLivePhotoView : KingfisherCompatible { }
  110. #endif
  111. #if os(tvOS) && canImport(TVUIKit)
  112. @available(tvOS 12.0, *)
  113. extension TVMonogramView : KingfisherCompatible { }
  114. #endif
  115. #if canImport(CarPlay) && !targetEnvironment(macCatalyst)
  116. @available(iOS 14.0, *)
  117. extension CPListItem : KingfisherCompatible { }
  118. #endif