0
我想每1分鐘清除默認緩存目錄中的所有圖像,但緩存文件沒有擴展名以指定它們的類型,我不知道如何刪除像PNG(而不是其他數據)的圖像。 這是示例代碼我在這個網站看到:如何清除緩存目錄中的圖像?
let fileManager = FileManager.default
let documentsUrl = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first! as NSURL
let documentsPath = documentsUrl.path
do {
if let documentPath = documentsPath
{
let fileNames = try fileManager.contentsOfDirectory(atPath: "\(documentPath)")
print("all files in cache: \(fileNames)")
for fileName in fileNames {
if (fileName.hasSuffix(".png"))
{
let filePathName = "\(documentPath)/\(fileName)"
try fileManager.removeItem(atPath: filePathName)
}
}
let files = try fileManager.contentsOfDirectory(atPath: "\(documentPath)")
print("all files in cache after deleting images: \(files)")
}
} catch {
print("Could not clear temp folder: \(error)")
}
如何重命名默認緩存系統將要緩存的映像? – Omnia
如果我把它們放在一個單獨的文件夾中,如果發生內存警告,iOS可以刪除它們嗎?或者我應該手動處理它們? – Omnia
ios中默認緩存文件夾中的圖像除外,什麼是緩存?還有別的事嗎? – Omnia