0

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)
        }
    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Yogesh Patel
  • 1,893
  • 1
  • 20
  • 55

1 Answers1

1

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"))
Ketan Sodvadiya
  • 464
  • 5
  • 11
  • Thank You Ketan for your response I will check and inform you ! – Yogesh Patel Mar 14 '19 at 07:46
  • Hello ketan can you please tell me what is difference between my code and your code because i not getting .? Thank You :) – Yogesh Patel Mar 16 '19 at 12:14
  • here you are trying to save only PNG file data and retrieve time you are trying to get image of content so that was difference you can check hole cod. and i am sure this my code are on working condition i have created one POC and gets work. – Ketan Sodvadiya Mar 18 '19 at 03:16
  • okay thanks for your explanation ! I will check this and inform you !! :) – Yogesh Patel Mar 18 '19 at 07:09