我正在将 firebase 图像保存在文档目录中!为了唯一性,我的文档目录名称是 firebase 图像名称!我检查了一个条件,如果我的文档目录中不存在 firebase 图像名称,则将该图像保存在文档目录中!
- 我正在检查该 firebase 名称是否意味着它保存在文档目录中,然后它获取文档目录图像,如果不是,那么它从 Firebase 获取图像!
问题是当我尝试获取图像时:-(1)例如:- Firebase 图像名称为 1.jpg 。(2) 文件目录保存图片,firebase 名称如 1.jpg 。(3) 现在我将 firebase 图像更改为 other 但它以名称 1.jpg 保存。(4) 当我尝试获取图像时,因为 1.jpg 已经在文档目录中,这就是为什么它没有返回我更新的图像它显示我以前的 1.jpg 图像!
我该如何解决这个问题。谢谢你
保存并获取图像代码:-
func saveImageDocumentDirectory(imageName: String){
let documentPath = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let imgURL = documentPath.appendingPathComponent(imageName, isDirectory: true)
if !FileManager.default.fileExists(atPath: imgURL.path){
do{
let image = UIImage(named: imgURL.path)
try image?.pngData()?.write(to: imgURL)
}catch let err{
print("error in save:\(err.localizedDescription)")
}
}
}
func getDocumentImage(imageName: String, firebaseImgURL: URL, imgView:UIImageView){
let documentPath = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let imgURL = documentPath.appendingPathComponent(imageName, isDirectory: true)
let image = UIImage(contentsOfFile: imgURL.path)
if image != nil{
imgView.image = image
}else{
imgView.kf.setImage(with: firebaseImgURL)
}
}