KFImage.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // KFImage.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2019/06/26.
  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. #if canImport(SwiftUI)
  27. import SwiftUI
  28. @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
  29. public struct KFImage: KFImageProtocol {
  30. public typealias HoldingView = Image
  31. public var context: Context<HoldingView>
  32. public init(context: Context<Image>) {
  33. self.context = context
  34. }
  35. }
  36. // MARK: - Image compatibility.
  37. @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
  38. extension KFImage {
  39. /// Configures current image with a `block`. This block will be lazily applied when creating the final `Image`.
  40. /// - Parameter block: The block applies to loaded image.
  41. /// - Returns: A `KFImage` view that configures internal `Image` with `block`.
  42. public func configure(_ block: @escaping (Image) -> Image) -> KFImage {
  43. var result = self
  44. result.context.configurations.append(block)
  45. return result
  46. }
  47. public func resizable(
  48. capInsets: EdgeInsets = EdgeInsets(),
  49. resizingMode: Image.ResizingMode = .stretch) -> KFImage
  50. {
  51. configure { $0.resizable(capInsets: capInsets, resizingMode: resizingMode) }
  52. }
  53. public func renderingMode(_ renderingMode: Image.TemplateRenderingMode?) -> KFImage {
  54. configure { $0.renderingMode(renderingMode) }
  55. }
  56. public func interpolation(_ interpolation: Image.Interpolation) -> KFImage {
  57. configure { $0.interpolation(interpolation) }
  58. }
  59. public func antialiased(_ isAntialiased: Bool) -> KFImage {
  60. configure { $0.antialiased(isAntialiased) }
  61. }
  62. }
  63. // MARK: - Deprecated
  64. @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
  65. extension KFImage {
  66. /// Creates a Kingfisher compatible image view to load image from the given `Source`.
  67. /// - Parameter source: The image `Source` defining where to load the target image.
  68. /// - Parameter options: The options should be applied when loading the image.
  69. /// Some UIKit related options (such as `ImageTransition.flip`) are not supported.
  70. /// - Parameter isLoaded: Whether the image is loaded or not. This provides a way to inspect the internal loading
  71. /// state. `true` if the image is loaded successfully. Otherwise, `false`. Do not set the
  72. /// wrapped value from outside.
  73. /// - Deprecated: Some options are not available in SwiftUI yet. Use `KFImage(source:isLoaded:)` to create a
  74. /// `KFImage` and configure the options through modifier instead. See methods of `KFOptionSetter`
  75. /// for more.
  76. @available(*, deprecated, message: "Some options are not available in SwiftUI yet. Use `KFImage(source:isLoaded:)` to create a `KFImage` and configure the options through modifier instead.")
  77. public init(source: Source?, options: KingfisherOptionsInfo? = nil, isLoaded: Binding<Bool> = .constant(false)) {
  78. let binder = KFImage.ImageBinder(source: source, options: options, isLoaded: isLoaded)
  79. self.init(binder: binder)
  80. }
  81. /// Creates a Kingfisher compatible image view to load image from the given `URL`.
  82. /// - Parameter url: The image URL from where to load the target image.
  83. /// - Parameter options: The options should be applied when loading the image.
  84. /// Some UIKit related options (such as `ImageTransition.flip`) are not supported.
  85. /// - Parameter isLoaded: Whether the image is loaded or not. This provides a way to inspect the internal loading
  86. /// state. `true` if the image is loaded successfully. Otherwise, `false`. Do not set the
  87. /// wrapped value from outside.
  88. /// - Deprecated: Some options are not available in SwiftUI yet. Use `KFImage(_:isLoaded:)` to create a
  89. /// `KFImage` and configure the options through modifier instead. See methods of `KFOptionSetter`
  90. /// for more.
  91. @available(*, deprecated, message: "Some options are not available in SwiftUI yet. Use `KFImage(_:isLoaded:)` to create a `KFImage` and configure the options through modifier instead.")
  92. init(_ url: URL?, options: KingfisherOptionsInfo? = nil, isLoaded: Binding<Bool> = .constant(false)) {
  93. self.init(source: url?.convertToSource(), options: options, isLoaded: isLoaded)
  94. }
  95. }
  96. #if DEBUG
  97. @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
  98. struct KFImage_Previews : PreviewProvider {
  99. static var previews: some View {
  100. Group {
  101. KFImage.url(URL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher/master/images/logo.png")!)
  102. .onSuccess { r in
  103. print(r)
  104. }
  105. .resizable()
  106. .aspectRatio(contentMode: .fit)
  107. .padding()
  108. }
  109. }
  110. }
  111. #endif
  112. #endif