OGeek|极客世界-中国程序员成长平台

标题: ios - 我们可以将本地镜像设置为 SDWebImage 的离线图像吗? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:41
标题: ios - 我们可以将本地镜像设置为 SDWebImage 的离线图像吗?

我正在使用

pod 'SDWebImage'

要在我的应用程序中下载图像,我发现所有图像都下载到 “/Library/Caches/com.bundlename.bundlename/com.alamofire.imagedownloader/fsCachedData” 具有一些独特的名称。

我的应用程序具有离线支持,并且我已经使用文件路径(文件位于库缓存目录中)管理图像的 CoreData 实体,当我上传我的图像时,我的服务器将响应我的 S3 文件 URL 以供下载。

因为我(上传文件的用户)已经有一个图像文件,但 SDWebImage 不知道。所以它会再次下载文件。

有什么建议我应该怎么做才能在不再次下载相同图像的情况下对其进行管理?我无法在数据库中保留本地路径,因为我的应用具有与多个设备的同步功能。

谢谢



Best Answer-推荐答案


我认为您可以为此目的使用 SDImageCache。 示例代码:

NSArray<NSString*> *urls = @"aws s3 url here";
    [[SDImageCache sharedImageCache] diskImageExistsWithKey:urls[0] completion:^(BOOL isInCache) {
        if ( isInCache ) {
            [yourImage setImage:[[SDImageCache sharedImageCache] imageFromDiskCacheForKey:urls[0]]];
        } else {
            NSURL *url = [NSURL URLWithString:item.url];
            [yourImage sd_setImageWithURL:url completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                [[SDImageCache sharedImageCache] storeImage:image forKey:urls[0] toDisk:YES completion:^{

                }];
                [yourImage setImage:image];
            }];
        }
    }] ;

关于ios - 我们可以将本地镜像设置为 SDWebImage 的离线图像吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44062389/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4