ImageLoader is a lightweight Swift SDK designed to load images asynchronously from a URL and cache them using NSCache
. It also provides an extension to UIImageView
in UIKit, making it easy to load and display images in your app with caching enabled.
- Asynchronously loads images from a URL.
- Caches images using
NSCache
to minimize network usage. - Provides a convenient
UIImageView
extension for seamless integration with UIKit. - Automatically handles image loading cancellation, making it ideal for use in table or collection views.
- Provides error handling and allows setting a placeholder image in case of failure.
To add ImageLoader to your project: Add the repository URL to your project’s package dependencies.
Using ImageLoader, you can load an image from a URL and cache it with just a few lines of code. The library provides an extension for UIImageView
to simplify the process.
let imageView = UIImageView()
let url = NSURL(string: "https://example.com/image.png")!
do {
try imageView.loadImage(from: url, errorPlaceholderImage: UIImage(named: "placeholder"))
} catch {
print("Failed to load image: \(error.localizedDescription)")
}
NOTE : You can also load a thumbnail of the loaded image into the image view as below. But it requires the minimum os target iOS 15.0.
do {
try imageView.loadThumbnail(from: url, of: CGSize(width: 100, height: 160), errorPlaceholderImage: UIImage(named: "placeholder"))
} catch {
print("Failed to load image thumbnail: \(error.localizedDescription)")
}
In cases where the UIImageView
is reused (e.g., in a table or collection view), you may want to cancel any ongoing image loading.
imageView.cancelLoading()
Errors during image loading (e.g., network issues) are captured in the custom CachedImageLoaderError
enum:
.errorLoading(Error)
: An error occurred during the download..errorDecoding
: The downloaded data could not be decoded as an image..cancelled
: The operation was manually cancelled.
To effectively use this SDK with reusable views like UITableViewCell
, you should cancel image loading before the cell is reused.
override func prepareForReuse() {
super.prepareForReuse()
imageView.cancelLoading()
}
The UIImageLoader.shared
instance manages image loading and caching. By default, images are cached in memory using NSCache
, making subsequent requests faster.
If an error occurs during image loading, you can specify a custom placeholder image:
let placeholder = UIImage(named: "errorPlaceholder")
try? imageView.loadImage(from: url, errorPlaceholderImage: placeholder)
-
loadImage(from url: NSURL, completion: @escaping (Result<UIImage, Error>) -> Void) -> UUID?
Loads an image from the provided URL and caches it if successful. Returns aUUID
for the request which can be used to cancel the operation. -
cancelLoading(id: UUID)
Cancels an ongoing image load operation associated with the providedUUID
.
-
load(from url: NSURL, for imageView: UIImageView, errorPlaceholderImage: UIImage? = nil)
Loads an image from the URL and sets it in the providedUIImageView
. -
cancel(for imageView: UIImageView)
Cancels the image loading operation for the specifiedUIImageView
. -
cancelAll()
Cancels all ongoing image loading operations.
-
loadImage(from url: NSURL, errorPlaceholderImage: UIImage? = nil)
Loads an image directly into theUIImageView
usingUIImageLoader
. -
cancelLoading()
Cancels the image loading operation for theUIImageView
.