简体   繁体   English

缓存图像以供离线使用 SDWebImage

[英]Cache image for use offline SDWebImage

I am using SDWebImage to download images from different URL's, now I was wondering if it is possible to download these images store them some where so when I am offline and launch my app I can still see them?我正在使用 SDWebImage 从不同的 URL 下载图像,现在我想知道是否可以下载这些图像并将它们存储在某个位置,这样当我离线并启动我的应用程序时,我仍然可以看到它们吗?

I noticed Instagram uses a similar thing, once you have downloaded the images you can close the app go offline and then reopen the app and you can still see the images.我注意到 Instagram 使用了类似的东西,一旦你下载了图像,你可以关闭应用程序离线,然后重新打开应用程序,你仍然可以看到图像。

I think this is possible with SDWebImage but not sure, please do correct me if it is not possible.我认为这对 SDWebImage 是可能的,但不确定,如果不可能,请纠正我。

Many thanks in advance to anyone that can help!非常感谢任何可以提供帮助的人!

import the UIImageView+WebCache.h header, and call the setImageWithURL:placeholderImage: method in your viewcontroller.导入UIImageView+WebCache.h标头,并在您的视图控制器中调用setImageWithURL:placeholderImage:方法。 SDWebImage lib will be handled for you, from async downloads to caching management. SDWebImage库将为您处理,从异步下载到缓存管理。

I assume what you want to archive is that kind of feed, where you have like 4-5 pictures shown after your App has been killed/restarted and you don't have an internet connection - like Instagram does.我假设您要存档的是那种提要,在您的应用程序被杀死/重新启动后,您会显示 4-5 张图片,并且您没有互联网连接 - 就像 Instagram 那样。

A possibility for you would be, to download images as you're used to when you have an internet connection and store like 4-5 images into your core data as NSData.对您来说,一种可能性是,当您有互联网连接时,按照习惯下载图像,并将 4-5 个图像作为 NSData 存储到您的核心数据中。

Then you are able to use those Images as placeholders and you would have "content" while the user has no connection.然后您就可以将这些图像用作占位符,并且在用户没有连接时您将拥有“内容”。

Here is code to convert an image to NSData and back:这是将图像转换为 NSData 并返回的代码:

let dataFromImage = UIImagePNGRepresentation(image!)!
let imageFromData = UIImage(data: imageData)

And here is a perfect tutorial of how to store images into core data.这是一个关于如何将图像存储到核心数据的完美教程

Then you are able to populate the tableView (for example) with images from your core data in case reachability == false .然后,您可以使用核心数据中的图像填充 tableView(例如),以防reachability == false

You can store your image in app directory using SDWebImage instead of store manually.您可以使用SDWebImage将图像存储在应用程序目录中,而不是手动存储。 It'll be more simple while you gonna use SDImageCache .当您使用SDImageCache时,它会更简单。 diskImageExistsWithKey determines you whether your image is exists in location (in directory or in you tempfolder of app) with a callback. diskImageExistsWithKey确定您的图像是否存在于位置(在应用程序的目录或临时文件夹中)。 You just have check it!您只需检查一下! ... ...

[[SDImageCache sharedImageCache] diskImageExistsWithKey:localImagePath completion:^(BOOL isInCache) {
        if ( isInCache ) {
            image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:localPath]; // it returns a simple UIImage 
[yourImageView setImage:[[SDImageCache sharedImageCache] imageFromDiskCacheForKey:localPath]]; // or set image in your UIImageView
        } else {
            [yourImageView sd_setImageWithURL:[NSURL URLWithString:downloadPath] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                if (image) {
                    [[SDImageCache sharedImageCache] storeImage:image forKey:localPath toDisk:YES completion:^{ // This block will help you to store image from server to local directiry
                        NSLog(@"Downloaded");
                    }];
                }
            }];
        }
    }] ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM