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

标签 ios swift firebase document-directory

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

  • 我正在检查该 firebase 名称是否意味着它保存在文档目录中,然后它获取文档目录图像,如果不是,则它从 Firebase 获取图像!!

问题是当我尝试获取图像时:- (1) 例如:- Firebase 图片名称是 1.jpg 。 (2) 文档目录保存图像,Firebase 名称如 1.jpg 。 (3) 现在我将 firebase 图像更改为其他图像,但它以名称 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)
        }
    }

最佳答案

请试试下面的代码

 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)
    }

ImageView 中的卫星图像

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

关于ios - 无法在 swift 4 iOS 的文档目录中保存正确的 firebase 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55148616/

相关文章:

json - 执行segue swift 4

ios - 在 Swift 中一个接一个地调用异步任务(iOS)

iOS Firebase - 扇出时 -'InvalidPathValidation' ,原因 : '(child:) Must be a non-empty string and not contain ' .' ' #' ' $' ' [' or ' ]''

angular - NullInjectorError : No provider for InjectionToken angularfire2. 应用程序选项

swift - 使用多个字段更新 firebase 数据库(iOS,swift)而不覆盖现有数据

ios - AVURLAsset loadValuesAsynchronously 从不失败

android - Dart/Flutter - 调用/执行存储在属性的字符串变量中的方法

iphone - 如何在 iPhone 游戏应用程序中使用 OpenGL ES 用动画绘制直线?

swift - 无法将类型 'UIAlertAction' 的值转换为预期的参数类型 'UIViewController'?

iphone - 设置位于导航/选项卡栏之间的 UIScrollView 框架