Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios AlamofireImage下载到CoreData_Ios_Swift_Core Data_Alamofire - Fatal编程技术网

Ios AlamofireImage下载到CoreData

Ios AlamofireImage下载到CoreData,ios,swift,core-data,alamofire,Ios,Swift,Core Data,Alamofire,我想缓存来自json的图像和少量数据以使其持久化我知道Alamofire图像有它自己的chade方法,但我猜只有在应用程序处于活动状态时,当应用程序再次关闭和打开时,chache被清除,所以我必须再次下载图像。所以 如果我的图片是这样下载的 image.af_setImage(withURL: url) 储蓄是可能的 imageEntity.save(image.image) 不确定代码是否准确,但想法是将下载的图像集保存到CoreData中的图像。 如果coreData中存在图像,并且从i

我想缓存来自json的图像和少量数据以使其持久化我知道Alamofire图像有它自己的chade方法,但我猜只有在应用程序处于活动状态时,当应用程序再次关闭和打开时,chache被清除,所以我必须再次下载图像。所以 如果我的图片是这样下载的

image.af_setImage(withURL: url)
储蓄是可能的

imageEntity.save(image.image)
不确定代码是否准确,但想法是将下载的图像集保存到CoreData中的图像。
如果coreData中存在图像,并且从if not下载并保存到coreData中显示,那么这种方法背后的任何逻辑都不是为了存储图像数据或大数据(文件),而是为了存储字符串、日期、整数。。。就像一个普通的数据库

如果要保存数据,可以使用AlamofireImageCache并将CoreData实体中的图像标识符保存为字符串。这是最简单的方法

let imageCache = AutoPurgingImageCache(
    memoryCapacity: 100_000_000,
    preferredMemoryUsageAfterPurge: 60_000_000
)
imageCache.add(yourImage, withIdentifier: "YourImageIdentifier")
imageEntity.identifier = "YourImageIdentifier"
然后,如果您想获得图像,您可以执行以下操作,例如:

let cachedAvatar = imageCache.image(withIdentifier: imageEntity.identifier)
有关缓存的更多信息,请查看AlamofireImage文档:

更新

如果您想在硬盘上创建自己的ImageCache,这里有一个存储图像的示例:

func saveImage(image:UIImage){

     if let data = UIImagePNGRepresentation(image) {

            let documentPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]!
            let filename = documentPath.appendingPathComponent("YourImageName")
            try? data.write(to: filename)
     }
}

func getImage(ForName name:String) -> UIImage?{ 
    let documentPath =    FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]!   
    let filename = documentPath.appendingPathComponent("YourImageName")         
     return UIImage(contentsOf:URL(fileURLWithPath: filename)) 
 }
希望有帮助!
Pierre

AutoPurgingImageCache位于内存缓存中,这意味着当您退出应用程序时,它会清除所有缓存的图像。因此,如何使其从应用程序退出时继续运行您无法使其持续工作。尝试使用像SDWebImage这样的东西,它有更灵活的缓存,可以配置为在内存中或持久化。我几乎可以肯定,alamofireCache将映像存储在硬盘上,而不是内存中。是的,它使用设备存储空间。问题是,在应用关闭后,是否所有缓存都被删除,如果没有,那么应用程序处于脱机状态,它应该返回缓存图像。我很确定AF的图像缓存不会像您描述的那样被清除。更好地使用它有更灵活的缓存选项。