KFImage.swift 5.6 KB

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