无法在 swift 4 iOS 的文档目录中获取正确的 firebase 图像保存

Can not get proper firebase image save in document directory in swift 4 iOS

我正在文档目录中保存 firebase 图像!!为了唯一性,我的文档目录名称是 firebase 图像名称!我检查了一个条件,如果我的文档目录中不存在 firebase 图像名称,则将该图像保存在文档目录中!!

问题是当我尝试获取图像时:- (1) 例如:- Firebase 图像名称是 1.jpg 。 (2) 文档目录保存图像,Firebase 名称如 1.jpg 。 (3) 现在我将 firebase 图像更改为其他图像,但它以名称 1.jpg 保存。 (4) 当我尝试获取图像时,因为 1.jpg 已经在文档目录中,这就是为什么它不 return 我更新图像它显示以前的 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)
        }
    }

请尝试使用下面的代码

 Save Data

 let placeholderURL: URL =
 getDocumentDirectoryPath()!.appendingPathComponent("\(imageName.JPG)/",
 isDirectory: true)
 let fileExists: Bool = FileManager.default.fileExists(atPath: placeholderURL.path )


         if !fileExists {

             let thumbnailImageURL = URL(string: firebaseURL)
             var placeholderData: Data? = nil
             if let url = url {
                 do {

                    placeholderData = try Data(contentsOf: (thumbnailImageURL ?? nil)!)
                 } catch {
                     print("Unable to load data: \(error)")
                 }
             }
             if urlData != nil {
                 do {
                     //saving is done on main thread
                     try placeholderData?.write(to: URL(fileURLWithPath: placeholderURL.path) , options: .atomic)
                 } catch {
                     print(error)
                 }

            }
       }

检索图片

    let thumbnailURL: URL = getDocumentDirectoryPath()!.appendingPathComponent("\("imageName.JPG")/", isDirectory: true)

    let fileExists: Bool = FileManager.default.fileExists(atPath: thumbnailURL.path)


    var urlThumbnail:URL? = nil
    if fileExists {

        urlThumbnail = URL(fileURLWithPath: thumbnailURL.path)
    } else {

        urlThumbnail = URL(string: firebaseURL)
    }

图像视图中的卫星图像

self.imgtemp.sd_setImage(with: urlThumbnail, placeholderImage: UIImage(named: "placeholder.png"))