简体   繁体   中英

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

i'm saving firebase image in document directory !! For uniqueness my Document Directory name is firebase image name ! i have check with a condition that if firebase image name is not exists in my document directory then save that image in Document Directory !!

  • i'm checking if that firebase name means it save in document directory then it get document directory image if not then it get image from Firebase !!

Issue is when i try to get image :- (1) For example :- Firebase Image name is 1.jpg . (2) Document Directory save image with firebase name like 1.jpg . (3) now i change firebase image to other but it save with name 1.jpg . (4) when i try to get image because already 1.jpg is in Document Directory that's why it not return me updated image it show me previous 1.jpg image !!
how can i solved this issue. Thank You

Save And Get Image Code :-

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

Please try with like below code

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

            }
       }

Retrive image

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

Sat image in image view

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM