一款支持自定义 URLSession 的 SwiftUI AsyncImage 的即时替代方案
🖼️ RemoteImage
AsyncImage
的替代方案,支持自定义 URLSessions、缓存和动画阶段变化。
入门
通过 Swift 包管理器将 RemoteImage
添加到您的项目,并在需要使用该项目时添加 import RemoteImage
。
.package(url: "https://github.com/bdbergeron/RemoteImage", from: "1.0.0"),
用法
RemoteImage
的 API 旨在让您轻松地在您的应用/项目中采用它。在大多数情况下,它可以简单地替代 SwiftUI 的 AsyncImage
。
查看 Xcode 项目中的 RemoteImage Demos
应用,了解一些实时示例。
简单配置
let imageURL: URL?
/// A simple `RemoteImage` view.
RemoteImage(url: imageURL)
/// A simple `RemoteImage` view with modifier closure.
RemoteImage(url: imageURL) {
$0.resizable().scaledToFit()
}
/// A `RemoteImage` view with a custom placeholder view.
RemoteImage(url: imageURL) {
$0.resizable().scaledToFit()
} placeholder: {
ProgressView()
}
/// A `RemoteImage` view with custom placeholder and failure views.
RemoteImage(url: imageURL) {
$0.resizable().scaledToFit()
} placeholder: {
ProgressView()
} failure: { error in
ZStack {
Color.yellow.opacity(0.3)
Text("Image could not be loaded.")
.font(.caption)
.foregroundStyle(.secondary)
}
}
高级配置
let imageURL: URL?
let urlSession = URLSession(configuration: .ephemeral)
let imageCache = URLCache(memoryCapacity: 10_000_000, diskCapacity: 0)
/// Image loaded with a custom `URLSession` and using a custom phase transition animation.
RemoteImage(
url: imageURL,
urlSession: urlSession,
configuration: RemoteImageConfiguration(
transaction: Transaction(
animation: .spring(duration: 1.0).