Simple image loader in swift
To use it in your project, import the framework.
// Swift
import ImageLoader
- Simply load an image: This will automatically start the loading of the image.
ImageLoader.default.load("https://assets-cdn.github.com/images/modules/open_graph/github-mark.png") {
image, error in
self.imageView.image = image
}
Or use the UIImageView extension:
let imageView = UIImageView.imageView(with: "https://assets-cdn.github.com/images/modules/open_graph/github-mark.png")
// or
existinImageView.load(with: "https://assets-cdn.github.com/images/modules/open_graph/github-mark.png")
- Handle image request:
let imageRequest = ImageLoader.default.request("https://assets-cdn.github.com/images/modules/open_graph/github-mark.png") {
image, error in
self.imageView.image = image
}
You can cancel
/suspend
/restart
the request
imageRequest.cancel()
imageRequest.suspend()
imageRequest.start()
To monitor the request's progress, implement the ImageRequestDelegate
func progress(_ request: ImageRequest, totalBytesSent: Int64, totalBytesExpected: Int64) {
print("\(Double(totalBytesSent) / Double(totalBytesExpected) * 100.0)%")
}
If you do not want to start right away the loading of the image you can use the autoStart parameter
let imageRequest = ImageLoader.default.request("https://assets-cdn.github.com/images/modules/open_graph/github-mark.png", autoStart: false) {
image, error in
self.imageView.image = image
}
// Do Stuff
imageRequest.start()
By default images are cached, to disable that behavior:
let imageRequest = ImageLoader.default("https://assets-cdn.github.com/images/modules/open_graph/github-mark.png") {
image, error in
self.imageView.image = image
}
imageRequest.shouldCacheResult = false