如何使用 swift 在 IOS 应用程序中缓存过期图像
How to cache images in IOS App with expiry age using swift
在 iOS 应用程序中,如何缓存具有指定有效期的图像?有存储和检索图片的例子,但是如何设置过期时间来自动删除旧图片?
网络就是网络,iOS就是iOS。如果要创建带过期的图片缓存,只能自己实现,或者使用开源库。我可以给你想法,实现起来并不难。因此,除了存储和检索功能之外,您还需要添加元数据管理方法,使用它您可以知道图像何时添加,该图像的到期日期是什么时候,以及某些事件发生的时间(应用程序激活,正在运行到背景等)你应该检查你的图像的元数据,如果过期日期就删除图像。就是这样,没什么难的。祝你好运!
P.S.: 在一些 git 源项目中我看到了您正在寻找的功能,请在 github 上查看 DFCache
,也许它适合您的需要.
如 Fahri 所述,您需要自己管理缓存(或使用开源库)。您可以轻松地创建一个缓存目录来存储您的图像。然后,在应用程序启动时,您解析此图像缓存目录以检查图像创建日期、检查时间并删除那些早于指定年龄的图像。
下面的 Swift 代码将完成这个 parsing/removing 工作,我将指定的年龄设置为 30,000(秒)
// We list the stored images in Caches/Images and delete old ones
let cacheDirectory = NSFileManager.defaultManager().URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask).first! as NSURL
let filelist = try? filemanager.contentsOfDirectoryAtPath(cacheDirectory.path!)
var newDir = cacheDirectory.URLByAppendingPathComponent("Images")
var properties = [NSURLLocalizedNameKey, NSURLCreationDateKey, NSURLLocalizedTypeDescriptionKey]
var URLlist = try? filemanager.contentsOfDirectoryAtURL(newDir, includingPropertiesForKeys: properties, options: [])
if URLlist != nil {
for URLname in URLlist! {
let filePath = URLname.path!
let attrFile: NSDictionary? = try? filemanager.attributesOfItemAtPath(filePath)
let createdAt = attrFile![NSFileCreationDate] as! NSDate
let createdSince = fabs( createdAt.timeIntervalSinceNow )
#if DEBUG
print( "file created at \(createdAt), \(createdSince) seconds ago" )
#endif
if createdSince > 30000 {
let resultDelete: Bool
do {
try filemanager.removeItemAtPath(filePath)
resultDelete = true
} catch _ {
resultDelete = false
}
#if DEBUG
print("purging file =\(filePath), result= \(resultDelete)")
#endif
}
}
}
在 iOS 应用程序中,如何缓存具有指定有效期的图像?有存储和检索图片的例子,但是如何设置过期时间来自动删除旧图片?
网络就是网络,iOS就是iOS。如果要创建带过期的图片缓存,只能自己实现,或者使用开源库。我可以给你想法,实现起来并不难。因此,除了存储和检索功能之外,您还需要添加元数据管理方法,使用它您可以知道图像何时添加,该图像的到期日期是什么时候,以及某些事件发生的时间(应用程序激活,正在运行到背景等)你应该检查你的图像的元数据,如果过期日期就删除图像。就是这样,没什么难的。祝你好运!
P.S.: 在一些 git 源项目中我看到了您正在寻找的功能,请在 github 上查看 DFCache
,也许它适合您的需要.
如 Fahri 所述,您需要自己管理缓存(或使用开源库)。您可以轻松地创建一个缓存目录来存储您的图像。然后,在应用程序启动时,您解析此图像缓存目录以检查图像创建日期、检查时间并删除那些早于指定年龄的图像。
下面的 Swift 代码将完成这个 parsing/removing 工作,我将指定的年龄设置为 30,000(秒)
// We list the stored images in Caches/Images and delete old ones
let cacheDirectory = NSFileManager.defaultManager().URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask).first! as NSURL
let filelist = try? filemanager.contentsOfDirectoryAtPath(cacheDirectory.path!)
var newDir = cacheDirectory.URLByAppendingPathComponent("Images")
var properties = [NSURLLocalizedNameKey, NSURLCreationDateKey, NSURLLocalizedTypeDescriptionKey]
var URLlist = try? filemanager.contentsOfDirectoryAtURL(newDir, includingPropertiesForKeys: properties, options: [])
if URLlist != nil {
for URLname in URLlist! {
let filePath = URLname.path!
let attrFile: NSDictionary? = try? filemanager.attributesOfItemAtPath(filePath)
let createdAt = attrFile![NSFileCreationDate] as! NSDate
let createdSince = fabs( createdAt.timeIntervalSinceNow )
#if DEBUG
print( "file created at \(createdAt), \(createdSince) seconds ago" )
#endif
if createdSince > 30000 {
let resultDelete: Bool
do {
try filemanager.removeItemAtPath(filePath)
resultDelete = true
} catch _ {
resultDelete = false
}
#if DEBUG
print("purging file =\(filePath), result= \(resultDelete)")
#endif
}
}
}