Code snippet that covers the most common tasks. Feel free to copy and paste them to your next great project.
@PageImage(purpose: card, source: "common-tasks-card"))
@PageColor(blue)
}
This documentation will describe some of the most common usage in general. The code snippet is based on iOS.
However, the similar code should also work for other platforms like macOS or tvOS, by replacing the corresponding class
(such as UIImage to NSImage, etc).
For common tasks of a specific part of Kingfisher, check the documentation below as well:
The view extension based APIs (for UIImageView, NSImageView, UIButton and NSButton) should be your first choice
whenever possible. It keeps your code simple and elegant.
URLlet url = URL(string: "https://example.com/image.jpg")
imageView.kf.setImage(with: url)
This simple code does these things:
url.absoluteString.imageView.image.url.UIImage object.imageView.image to display it.Later, when you call the setImage with the same url again, only step 1 and 2 will be performed, unless the cache is
purged.
let image = UIImage(named: "default_profile_icon")
imageView.kf.setImage(with: url, placeholder: image)
The image will show in the imageView while downloading from url.
You could also use a customized
UIVieworNSViewas placeholder, by conforming it toPlaceholder:> class MyView: UIView { /* Your implementation of view */ } > > extension MyView: Placeholder { /* Just leave it empty */} > > imageView.kf.setImage(with: url, placeholder: MyView()) > ``` > > The `MyView` instance will be added to / removed from the `imageView` as needed. ### Showing a loading indicator while downloadingswift imageView.kf.indicatorType = .activity imageView.kf.setImage(with: url)
Show a `UIActivityIndicatorView` in center of image view while downloading. ### Fading in downloaded imageswift imageView.kf.setImage(with: url, options: [.transition(.fade(0.2))])
### Completion handlerswift imageView.kf.setImage(with: url) { result in
// `result` is either a `.success(RetrieveImageResult)` or a `.failure(KingfisherError)` switch result { case .success(let value): // The image was set to image view: print(value.image)
// From where the image was retrieved:
// - .none - Just downloaded.
// - .memory - Got from memory cache.
// - .disk - Got from disk cache.
print(value.cacheType)
// The source object which contains information like `url`.
print(value.source)
case .failure(let error):
print(error) // The error happens
}
}
### Getting an image without setting to UI
Sometimes, you just want to get the image with Kingfisher instead of setting it to an image view. Use `KingfisherManager` for it:
swift KingfisherManager.shared.retrieveImage(with: url) { result in
// Do something with `result`
} ```