使用 combine 的 Publisher 在 SwiftUI 图像中从远程 URL 异步加载图像
Loading image from remote URL asynchronously in SwiftUI Image using combine's Publisher
我一直在寻找从远程服务器图像异步加载图像的好解决方案 URL。网上有很多解决方案。很遗憾 Apple 没有为如此常见的东西提供原生的功能。无论如何,我发现 Sundell's blog 真的很有趣,并从中吸取了一些好处来创建我自己的 ImageLoader,如下所示:
import Combine
class ImageLoader {
private let urlSession: URLSession
private let cache: NSCache<NSURL, UIImage>
init(urlSession: URLSession = .shared,
cache: NSCache<NSURL, UIImage> = .init()) {
self.urlSession = urlSession
self.cache = cache
}
func publisher(for url: URL) -> AnyPublisher<UIImage, Error> {
if let image = cache.object(forKey: url as NSURL) {
return Just(image)
.setFailureType(to: Error.self)
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
} else {
return urlSession
.dataTaskPublisher(for: url)
.map(\.data)
.tryMap { data in
guard let image = UIImage(data: data) else {
throw URLError(.badServerResponse, userInfo: [
NSURLErrorFailingURLErrorKey: url
])
}
return image
}
.receive(on: DispatchQueue.main)
.handleEvents(receiveOutput: { [cache] image in
cache.setObject(image, forKey: url as NSURL)
})
.eraseToAnyPublisher()
}
}
}
如您所见,发布者提供了 AnyPublisher<UIImage, Error>
的实例。我不完全确定如何在我的 MyImageView
中使用此 ImageLoader
,如下所示:
struct MyImageView: View {
var url: URL
var imageLoader = ImageLoader()
@State private var image = #imageLiteral(resourceName: "placeholder")
var body: some View {
Image(uiImage: image)
.onAppear {
let cancellable = imageLoader.publisher(for: url).sink(receiveCompletion: { failure in
print(failure) // doesn't print
}, receiveValue: { image in
self.image = image // not getting executed
})
cancellable.cancel() // tried with and without this line.
}
}
}
如何从 ImageLoader
发布者那里提取 UIImage
AnyPublisher<UIImage, Error>
的一个实例?
您必须使用 ObservableObject
订阅 ImageLoader 提供的发布者。
class ImageProvider: ObservableObject {
@Published var image = UIImage(named: "icHamburger")!
private var cancellable: AnyCancellable?
private let imageLoader = ImageLoader()
func loadImage(url: URL) {
self.cancellable = imageLoader.publisher(for: url)
.sink(receiveCompletion: { failure in
print(failure)
}, receiveValue: { image in
self.image = image
})
}
}
struct MyImageView: View {
var url: URL
@StateObject var viewModel = ImageProvider()
var body: some View {
Image(uiImage: viewModel.image)
.onAppear {
viewModel.loadImage(url: url)
}
}
}
因为我不想对一个简单的ImageLoader
进行过多的隔离,所以我让它符合ObservableObject
。所以,我只是修改@sElanthiraiyan 提供的答案。此外,通过更多研究,我发现发布者需要在需要时存储并在不再使用时释放。这是修改后的代码
ImageLoader:
class ImageLoader: ObservableObject {
@Published var image: UIImage
private var bag = Set<AnyCancellable>()
//...
init(placeholder: UIImage = UIImage(),
urlSession: URLSession = .shared,
cache: NSCache<NSURL, UIImage> = .init()) {
self.image = placeholder
//...
}
//...
func load(from url: URL) {
publisher(for: url)
.sink(receiveCompletion: { _ in })
{ image in
self.image = image
}
.store(in: &bag)
}
}
MyImageView:
struct MyImageView: View {
var url: URL
@StateObject var imageLoader = ImageLoader(placeholder: UIImage(named: "placeholder")!) // or @ObservedObject if iOS 13 support is required
var body: some View {
Image(uiImage: imageLoader.image)
.onAppear {
imageLoader.load(from: url)
}
}
}
iOS 15+
struct ContentView: View {
var body: some View {
if #available(iOS 15.0, *) {
AsyncImage(url: URL(string: "https://unsplash.com/photos/_ce9iqvtF90/download?force=true"), transaction: .init(animation: .spring())) { phase in
switch phase {
case .empty:
Color.green
.opacity(0.2)
.transition(.opacity.combined(with: .scale))
case .success(let image):
image
.resizable()
.aspectRatio(contentMode: .fill)
.transition(.opacity.combined(with: .scale))
case .failure(let error):
Color.red
@unknown default:
Color.yellow
}
}
.frame(width: 400, height: 266)
.mask(RoundedRectangle(cornerRadius: 16))
} else {
// Fallback on earlier versions
}
}
}
我一直在寻找从远程服务器图像异步加载图像的好解决方案 URL。网上有很多解决方案。很遗憾 Apple 没有为如此常见的东西提供原生的功能。无论如何,我发现 Sundell's blog 真的很有趣,并从中吸取了一些好处来创建我自己的 ImageLoader,如下所示:
import Combine
class ImageLoader {
private let urlSession: URLSession
private let cache: NSCache<NSURL, UIImage>
init(urlSession: URLSession = .shared,
cache: NSCache<NSURL, UIImage> = .init()) {
self.urlSession = urlSession
self.cache = cache
}
func publisher(for url: URL) -> AnyPublisher<UIImage, Error> {
if let image = cache.object(forKey: url as NSURL) {
return Just(image)
.setFailureType(to: Error.self)
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
} else {
return urlSession
.dataTaskPublisher(for: url)
.map(\.data)
.tryMap { data in
guard let image = UIImage(data: data) else {
throw URLError(.badServerResponse, userInfo: [
NSURLErrorFailingURLErrorKey: url
])
}
return image
}
.receive(on: DispatchQueue.main)
.handleEvents(receiveOutput: { [cache] image in
cache.setObject(image, forKey: url as NSURL)
})
.eraseToAnyPublisher()
}
}
}
如您所见,发布者提供了 AnyPublisher<UIImage, Error>
的实例。我不完全确定如何在我的 MyImageView
中使用此 ImageLoader
,如下所示:
struct MyImageView: View {
var url: URL
var imageLoader = ImageLoader()
@State private var image = #imageLiteral(resourceName: "placeholder")
var body: some View {
Image(uiImage: image)
.onAppear {
let cancellable = imageLoader.publisher(for: url).sink(receiveCompletion: { failure in
print(failure) // doesn't print
}, receiveValue: { image in
self.image = image // not getting executed
})
cancellable.cancel() // tried with and without this line.
}
}
}
如何从 ImageLoader
发布者那里提取 UIImage
AnyPublisher<UIImage, Error>
的一个实例?
您必须使用 ObservableObject
订阅 ImageLoader 提供的发布者。
class ImageProvider: ObservableObject {
@Published var image = UIImage(named: "icHamburger")!
private var cancellable: AnyCancellable?
private let imageLoader = ImageLoader()
func loadImage(url: URL) {
self.cancellable = imageLoader.publisher(for: url)
.sink(receiveCompletion: { failure in
print(failure)
}, receiveValue: { image in
self.image = image
})
}
}
struct MyImageView: View {
var url: URL
@StateObject var viewModel = ImageProvider()
var body: some View {
Image(uiImage: viewModel.image)
.onAppear {
viewModel.loadImage(url: url)
}
}
}
因为我不想对一个简单的ImageLoader
进行过多的隔离,所以我让它符合ObservableObject
。所以,我只是修改@sElanthiraiyan 提供的答案。此外,通过更多研究,我发现发布者需要在需要时存储并在不再使用时释放。这是修改后的代码
ImageLoader:
class ImageLoader: ObservableObject {
@Published var image: UIImage
private var bag = Set<AnyCancellable>()
//...
init(placeholder: UIImage = UIImage(),
urlSession: URLSession = .shared,
cache: NSCache<NSURL, UIImage> = .init()) {
self.image = placeholder
//...
}
//...
func load(from url: URL) {
publisher(for: url)
.sink(receiveCompletion: { _ in })
{ image in
self.image = image
}
.store(in: &bag)
}
}
MyImageView:
struct MyImageView: View {
var url: URL
@StateObject var imageLoader = ImageLoader(placeholder: UIImage(named: "placeholder")!) // or @ObservedObject if iOS 13 support is required
var body: some View {
Image(uiImage: imageLoader.image)
.onAppear {
imageLoader.load(from: url)
}
}
}
iOS 15+
struct ContentView: View {
var body: some View {
if #available(iOS 15.0, *) {
AsyncImage(url: URL(string: "https://unsplash.com/photos/_ce9iqvtF90/download?force=true"), transaction: .init(animation: .spring())) { phase in
switch phase {
case .empty:
Color.green
.opacity(0.2)
.transition(.opacity.combined(with: .scale))
case .success(let image):
image
.resizable()
.aspectRatio(contentMode: .fill)
.transition(.opacity.combined(with: .scale))
case .failure(let error):
Color.red
@unknown default:
Color.yellow
}
}
.frame(width: 400, height: 266)
.mask(RoundedRectangle(cornerRadius: 16))
} else {
// Fallback on earlier versions
}
}
}