NMAImageFetch
is library for downloading and caching images efficiently using UIKit
or SwiftUI
.
- iOS 12.0+
- File > Swift Packages > Add Package Dependency
- Add
https://github.com/nomasystems/nimagefetch-ios.git
- Select "Up to next major" with "2.0.0"
let imageView: NImageFetchView = .init(frame: .zero)
imageView.setImage(from: URLRequest(url: url))
let imageView: UIImageView = .init(frame: .zero)
let imageFetchRequest = ImageFetchRequest(urlRequest: URLRequest(url: url))
_ = ImageFetch.shared.requestImage(imageFetchRequest) { result in
switch result {
case let .success(uiImage, _):
self.imageView.image = uiImage
case .failure(let error):
switch error {
case .cancelled:
break
case let .networkError(err):
print(err?.localizedDescription ?? "unknown network error")
}
}
}
let imageView: UIImageView = .init(frame: .zero)
let imageFetchRequest = ImageFetchRequest(urlRequest: URLRequest(url: url))
let task = ImageFetch.shared.requestImage(fetchRequest) { _ in }
if let task = task {
ImageFetch.shared.cancel(task)
}
Using async/await
await ImageFetch.shared.purgeCaches()
Using completion closures
await ImageFetch.shared.purgeCaches() {
// Called when purge finishes
}
ImageFetchView(url: url, animated: .always, fallbackImage: nil)
ImageFetchView(urlRequest: URLRequest(url: url), animated: .ifAsync, fallbackImage: UIImage(named: "..."))
ImageFetchView(imageFetchRequest: ImageFetchRequest(urlRequest: URLRequest(url: url)), animated: .never, fallbackImage: nil)
Any doubt or suggestion? Please check out our issue tracker.