Add all the Image struct method into WebImage
This commit is contained in:
parent
a37f20957f
commit
e8939701e6
|
@ -6,8 +6,8 @@
|
||||||
"repositoryURL": "https://github.com/SDWebImage/SDWebImage.git",
|
"repositoryURL": "https://github.com/SDWebImage/SDWebImage.git",
|
||||||
"state": {
|
"state": {
|
||||||
"branch": null,
|
"branch": null,
|
||||||
"revision": "0a3cd255a655b73fb3b3437acf2ab506b5c0c9c6",
|
"revision": "9c1682e37bf3486daccd313fcbcd7fd90a2064f4",
|
||||||
"version": "5.1.0"
|
"version": "5.2.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
@ -14,10 +14,10 @@ class ImageManager : ObservableObject {
|
||||||
|
|
||||||
var objectWillChange = PassthroughSubject<ImageManager, Never>()
|
var objectWillChange = PassthroughSubject<ImageManager, Never>()
|
||||||
|
|
||||||
private var manager = SDWebImageManager.shared
|
var manager = SDWebImageManager.shared
|
||||||
private weak var currentOperation: SDWebImageOperation? = nil
|
weak var currentOperation: SDWebImageOperation? = nil
|
||||||
|
|
||||||
var image: Image? {
|
var image: PlatformImage? {
|
||||||
willSet {
|
willSet {
|
||||||
objectWillChange.send(self)
|
objectWillChange.send(self)
|
||||||
}
|
}
|
||||||
|
@ -36,11 +36,7 @@ class ImageManager : ObservableObject {
|
||||||
func load() {
|
func load() {
|
||||||
currentOperation = manager.loadImage(with: url, options: options, context: context, progress: nil) { (image, data, error, cacheType, _, _) in
|
currentOperation = manager.loadImage(with: url, options: options, context: context, progress: nil) { (image, data, error, cacheType, _, _) in
|
||||||
if let image = image {
|
if let image = image {
|
||||||
#if os(macOS)
|
self.image = image
|
||||||
self.image = Image(nsImage: image)
|
|
||||||
#else
|
|
||||||
self.image = Image(uiImage: image)
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,22 @@
|
||||||
import Foundation
|
import Foundation
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
|
#if os(macOS)
|
||||||
|
typealias PlatformImage = NSImage
|
||||||
|
#else
|
||||||
|
typealias PlatformImage = UIImage
|
||||||
|
#endif
|
||||||
|
|
||||||
|
extension Image {
|
||||||
|
init(platformImage: PlatformImage) {
|
||||||
|
#if os(macOS)
|
||||||
|
self.init(nsImage: platformImage)
|
||||||
|
#else
|
||||||
|
self.init(uiImage: platformImage)
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#if !os(watchOS)
|
#if !os(watchOS)
|
||||||
|
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
|
|
|
@ -15,6 +15,8 @@ public struct WebImage : View {
|
||||||
public var options: SDWebImageOptions
|
public var options: SDWebImageOptions
|
||||||
public var context: [SDWebImageContextOption : Any]?
|
public var context: [SDWebImageContextOption : Any]?
|
||||||
|
|
||||||
|
var configurations: [(Image) -> Image] = []
|
||||||
|
|
||||||
@ObservedObject var imageManager: ImageManager
|
@ObservedObject var imageManager: ImageManager
|
||||||
|
|
||||||
public init(url: URL?, placeholder: Image? = nil, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil) {
|
public init(url: URL?, placeholder: Image? = nil, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil) {
|
||||||
|
@ -26,26 +28,56 @@ public struct WebImage : View {
|
||||||
}
|
}
|
||||||
|
|
||||||
public var body: some View {
|
public var body: some View {
|
||||||
if let image = imageManager.image {
|
let image: Image
|
||||||
return image
|
if let platformImage = imageManager.image {
|
||||||
.resizable()
|
image = Image(platformImage: platformImage)
|
||||||
.onAppear {}
|
} else if let placeholder = placeholder {
|
||||||
.onDisappear {}
|
image = placeholder
|
||||||
} else if let image = placeholder {
|
|
||||||
return image
|
|
||||||
.resizable()
|
|
||||||
.onAppear { self.imageManager.load() }
|
|
||||||
.onDisappear { self.imageManager.cancel() }
|
|
||||||
} else {
|
} else {
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
let emptyImage = Image(nsImage: NSImage())
|
let emptyImage = Image(nsImage: NSImage())
|
||||||
#else
|
#else
|
||||||
let emptyImage = Image(uiImage: UIImage())
|
let emptyImage = Image(uiImage: UIImage())
|
||||||
#endif
|
#endif
|
||||||
return emptyImage
|
image = emptyImage
|
||||||
.resizable()
|
}
|
||||||
.onAppear { self.imageManager.load() }
|
return configurations.reduce(image) { (previous, configuration) in
|
||||||
.onDisappear { self.imageManager.cancel() }
|
configuration(previous)
|
||||||
|
}
|
||||||
|
.onAppear {
|
||||||
|
if self.imageManager.image == nil {
|
||||||
|
self.imageManager.load()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.onDisappear {
|
||||||
|
self.imageManager.cancel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension WebImage {
|
||||||
|
func configure(_ block: @escaping (Image) -> Image) -> WebImage {
|
||||||
|
var result = self
|
||||||
|
result.configurations.append(block)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
public func resizable(
|
||||||
|
capInsets: EdgeInsets = EdgeInsets(),
|
||||||
|
resizingMode: Image.ResizingMode = .stretch) -> WebImage
|
||||||
|
{
|
||||||
|
configure { $0.resizable(capInsets: capInsets, resizingMode: resizingMode) }
|
||||||
|
}
|
||||||
|
|
||||||
|
public func renderingMode(_ renderingMode: Image.TemplateRenderingMode?) -> WebImage {
|
||||||
|
configure { $0.renderingMode(renderingMode) }
|
||||||
|
}
|
||||||
|
|
||||||
|
public func interpolation(_ interpolation: Image.Interpolation) -> WebImage {
|
||||||
|
configure { $0.interpolation(interpolation) }
|
||||||
|
}
|
||||||
|
|
||||||
|
public func antialiased(_ isAntialiased: Bool) -> WebImage {
|
||||||
|
configure { $0.antialiased(isAntialiased) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue