First of all, great work here @onevcat 馃嵒
I was wondering if Kingfisher allows for loading and caching images from disk (local paths) as well as downloading. Maybe I skipped some part of the docs where it's said, but I can't quite find it.
Hi,
The main purpose of Kingfisher is downloading/caching images from web. Since you can use the components in Kingfisher separately, there is an ImageCache type to help you manage the cache for images, either from web or local. However, there is not a component to load a local image directly currently. You may need to use image APIs from Cocoa/CocoaTouch to load an image and then send it to the ImageCache, if it is not already in the cache.
Sounds about right. Would you be interested in somebody implementing this particular feature? I think it'd be a nice thing to have within the main framework.
Hi,
Thanks for considering to contribute! We could discuss it.
In fact I am not quite sure about whether it could be useful to have a feature to load from local disk. It will not boost your loading process.
If you are using UIImage(named:), the image will automatically cached in memory. If you are using a path or data object to create an image, it could be also very easy to implement a cache by using NSCache, instead of introducing a lot of unrelated (web-oriented) code from Kingfisher.
Could I know something more your use case? Why do you need to cache an image from disk which is already there?
In my case, my app handles images coming from both disk and web (like iCloud Photo Library does), so it'd pretty convenient not to have to worry about the source of the images when loading and displaying them. I could build some abstraction on top of Kingfisher and my own local caching mechanism, but I like how other caches provide this out of the box (e.g. Haneke).
Ah,
Yes, it makes sense that you have a mixed source of images.
Sure, it could be a useful feature in your case to have a framework to handle. Since I am not quite familiar with loading an image from photo library or iCloud, I will investigate them first to see whether we could keep using current API for it as well as make it back compatible. If not, we may need to add some new APIs for it.
And cool, if you'd like, a p-r for this will be warmly welcome! :)
I wasn't actually meaning loading photos from iCloud or the photo library, but rather loading them from a local path (Documents folder). I don't believe this would require any breaking changes, just a new mechanism similar to KingfisherManager.sharedManager.downloader for local images.
Hi there,
I would like to be able to determine synchronously whether I can immediately access a cached image. I don't want to wait for an async block to complete (albeit very quickly if cached鈥攐r at indeterminate length if downloading) before deciding how to proceed. I think this is along the lines of your discussion. Any suggestions? Thanks!
The ImageCache for disk is not using the main queue for IO, so there is no synchronous way to load image from disk safely. The only reliable method of loading an image is retrieveImageForKey(_:options:completionHandler:). You could use it at any time to load an image correctly, even you just add it into the cache.
First of all, great work here @onevcat 馃嵒
I was wondering if Kingfisher allows for loading and caching images from disk (local paths) as well as downloading. Maybe I skipped some part of the docs where it's said, but I can't quite find it.
@onevcat @DavdRoman
i think you need this for locally image show on image view:-
extension KingfisherWrapper where Base: ImageView{
public func setLocalImage(withUrl url: URL?){
if let url = url{
do {
let imageData = try Data(contentsOf: url)
self.base.image = UIImage(data: imageData)
} catch {
print("Unable to load data: \(error)")
}
}
}
}
@jaipee1 Thanks for commenting. But now for Kingfisher 5, we have a LocalFileImageDataProvider for this task, which works better and seamlessly with existing Kingfisher features. See this.
Most helpful comment
@jaipee1 Thanks for commenting. But now for Kingfisher 5, we have a
LocalFileImageDataProviderfor this task, which works better and seamlessly with existing Kingfisher features. See this.