KFImage.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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) && canImport(Combine)
  27. import SwiftUI
  28. @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.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 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
  36. extension Image: KFImageHoldingView {
  37. public static func created(from image: KFCrossPlatformImage) -> Image {
  38. if #available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
  39. return Image(crossPlatformImage: image)
  40. } else {
  41. #if canImport(UIKit)
  42. // The CG image is used to solve #1395
  43. // It should be not necessary if SwiftUI.Image can handle resizing correctly when created
  44. // by `Image.init(uiImage:)`. (The orientation information should be already contained in
  45. // a `UIImage`)
  46. // https://github.com/onevcat/Kingfisher/issues/1395
  47. //
  48. // This issue happens on iOS 13 and was fixed by Apple from iOS 14.
  49. if let cgImage = image.cgImage {
  50. return Image(decorative: cgImage, scale: image.scale, orientation: image.imageOrientation.toSwiftUI())
  51. } else {
  52. return Image(crossPlatformImage: image)
  53. }
  54. #else
  55. return Image(crossPlatformImage: image)
  56. #endif
  57. }
  58. }
  59. }
  60. // MARK: - Image compatibility.
  61. @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
  62. extension KFImage {
  63. public func resizable(
  64. capInsets: EdgeInsets = EdgeInsets(),
  65. resizingMode: Image.ResizingMode = .stretch) -> KFImage
  66. {
  67. configure { $0.resizable(capInsets: capInsets, resizingMode: resizingMode) }
  68. }
  69. public func renderingMode(_ renderingMode: Image.TemplateRenderingMode?) -> KFImage {
  70. configure { $0.renderingMode(renderingMode) }
  71. }
  72. public func interpolation(_ interpolation: Image.Interpolation) -> KFImage {
  73. configure { $0.interpolation(interpolation) }
  74. }
  75. public func antialiased(_ isAntialiased: Bool) -> KFImage {
  76. configure { $0.antialiased(isAntialiased) }
  77. }
  78. }
  79. // MARK: - Deprecated
  80. @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
  81. extension KFImage {
  82. /// Creates a Kingfisher compatible image view to load image from the given `Source`.
  83. /// - Parameter source: The image `Source` defining where to load the target image.
  84. /// - Parameter options: The options should be applied when loading the image.
  85. /// Some UIKit related options (such as `ImageTransition.flip`) are not supported.
  86. /// - Parameter isLoaded: Whether the image is loaded or not. This provides a way to inspect the internal loading
  87. /// state. `true` if the image is loaded successfully. Otherwise, `false`. Do not set the
  88. /// wrapped value from outside.
  89. /// - Deprecated: Some options are not available in SwiftUI yet. Use `KFImage(source:isLoaded:)` to create a
  90. /// `KFImage` and configure the options through modifier instead. See methods of `KFOptionSetter`
  91. /// for more.
  92. @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.")
  93. public init(source: Source?, options: KingfisherOptionsInfo? = nil, isLoaded: Binding<Bool> = .constant(false)) {
  94. let binder = KFImage.ImageBinder(source: source, options: options, isLoaded: isLoaded)
  95. self.init(binder: binder)
  96. }
  97. /// Creates a Kingfisher compatible image view to load image from the given `URL`.
  98. /// - Parameter url: The image URL from where to load the target image.
  99. /// - Parameter options: The options should be applied when loading the image.
  100. /// Some UIKit related options (such as `ImageTransition.flip`) are not supported.
  101. /// - Parameter isLoaded: Whether the image is loaded or not. This provides a way to inspect the internal loading
  102. /// state. `true` if the image is loaded successfully. Otherwise, `false`. Do not set the
  103. /// wrapped value from outside.
  104. /// - Deprecated: Some options are not available in SwiftUI yet. Use `KFImage(_:isLoaded:)` to create a
  105. /// `KFImage` and configure the options through modifier instead. See methods of `KFOptionSetter`
  106. /// for more.
  107. @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.")
  108. init(_ url: URL?, options: KingfisherOptionsInfo? = nil, isLoaded: Binding<Bool> = .constant(false)) {
  109. self.init(source: url?.convertToSource(), options: options, isLoaded: isLoaded)
  110. }
  111. }
  112. #if DEBUG
  113. @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
  114. struct KFImage_Previews : PreviewProvider {
  115. static var previews: some View {
  116. Group {
  117. KFImage.url(URL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher/master/images/logo.png")!)
  118. .onSuccess { r in
  119. print(r)
  120. }
  121. .resizable()
  122. .aspectRatio(contentMode: .fit)
  123. .padding()
  124. }
  125. }
  126. }
  127. #endif
  128. #endif