Add all the Image struct method into WebImage

This commit is contained in:
DreamPiggy 2019-10-01 12:22:51 +08:00
parent a37f20957f
commit e8939701e6
4 changed files with 68 additions and 24 deletions

View File

@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/SDWebImage/SDWebImage.git",
"state": {
"branch": null,
"revision": "0a3cd255a655b73fb3b3437acf2ab506b5c0c9c6",
"version": "5.1.0"
"revision": "9c1682e37bf3486daccd313fcbcd7fd90a2064f4",
"version": "5.2.0"
}
}
]

View File

@ -14,10 +14,10 @@ class ImageManager : ObservableObject {
var objectWillChange = PassthroughSubject<ImageManager, Never>()
private var manager = SDWebImageManager.shared
private weak var currentOperation: SDWebImageOperation? = nil
var manager = SDWebImageManager.shared
weak var currentOperation: SDWebImageOperation? = nil
var image: Image? {
var image: PlatformImage? {
willSet {
objectWillChange.send(self)
}
@ -36,11 +36,7 @@ class ImageManager : ObservableObject {
func load() {
currentOperation = manager.loadImage(with: url, options: options, context: context, progress: nil) { (image, data, error, cacheType, _, _) in
if let image = image {
#if os(macOS)
self.image = Image(nsImage: image)
#else
self.image = Image(uiImage: image)
#endif
self.image = image
}
}
}

View File

@ -9,6 +9,22 @@
import Foundation
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(macOS)

View File

@ -15,6 +15,8 @@ public struct WebImage : View {
public var options: SDWebImageOptions
public var context: [SDWebImageContextOption : Any]?
var configurations: [(Image) -> Image] = []
@ObservedObject var imageManager: ImageManager
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 {
if let image = imageManager.image {
return image
.resizable()
.onAppear {}
.onDisappear {}
} else if let image = placeholder {
return image
.resizable()
.onAppear { self.imageManager.load() }
.onDisappear { self.imageManager.cancel() }
let image: Image
if let platformImage = imageManager.image {
image = Image(platformImage: platformImage)
} else if let placeholder = placeholder {
image = placeholder
} else {
#if os(macOS)
let emptyImage = Image(nsImage: NSImage())
#else
let emptyImage = Image(uiImage: UIImage())
#endif
return emptyImage
.resizable()
.onAppear { self.imageManager.load() }
.onDisappear { self.imageManager.cancel() }
image = emptyImage
}
return configurations.reduce(image) { (previous, configuration) in
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) }
}
}