0

I'm downloading some cover images for books from the web and save them to Library/Caches/temporary. This works great, I've verified it width Finder, that the path is correct and the name of file is correct.

I can't seem to read the image from disc, though the path is correct and the image is for sure there.

I'm getting the URL for Library/Caches fresh on each run. I'm appending a constant temporary each time I need that folder(where the cover images are saved)

Getting the Library/Cache path

self.cachesDirectory = try FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: false)

Saving the image(AssetsHandler.sharedInstance)

func tempSaveCover(withDataAndISBN data: Data, isbn: String){

    if let image = UIImage(data: data){
        if let pngRepresentation = UIImagePNGRepresentation(image){
            let path = self.cachesDirectory.appendingPathComponent(Constants.AssetsHandler.tempCoverPath, isDirectory: true).appendingPathComponent(isbn).appendingPathExtension("png")

            do{
                try pngRepresentation.write(to: path)
            }catch let error{
            print("Can't write png to disc: \(error.localizedDescription)")
            }
        }
    }
}

This is how the URL looks like file:///Users/Joakim/Library/Developer/CoreSimulator/Devices/EBA74A27-B09C-4AFC-B825-ECDC37445053/data/Containers/Data/Application/A2B398CF-8535-492A-9787-D088C24ED661/Library/Caches/temporary/9788203196195.png

Reading the image(AssetsHandler.sharedInstance)

func tempGetCover(withISBNNumber isbn: String) -> UIImage{
    let path = self.cachesDirectory.appendingPathComponent(Constants.AssetsHandler.tempCoverPath, isDirectory: true).appendingPathComponent(isbn).appendingPathExtension("png")

        if let cover = FileManager.default.contents(atPath: path.absoluteString){
            if let image = UIImage(data: cover) {
                return image
            }
        }
    /*if let image = UIImage(contentsOfFile: path.absoluteString){
        return image
    }*/
    return UIImage(named: "default-image")!

}

Getting the image(Book class)

var coverImage: UIImage? {
    get{
        guard let idisbn = self.isbn else {return UIImage(named:"default-image")}

        /*if let image = UIImage(contentsOfFile: AssetsHandler.sharedInstance.path(for: idisbn).absoluteString){
            print("Image: ok")
            return image
        }*/
        return AssetsHandler.sharedInstance.tempGetCover(withISBNNumber: idisbn)
        //return UIImage(named:"default-image")
    }
    set(image){
        self.coverImage = image
    }
}

I have verified that the passing argument ISBN is the same. Also that the image are saved before I try to read it.

Any ideas why I'm only able to display the default-image ?

JoakimE
  • 1,820
  • 1
  • 21
  • 30
  • Have you tried another directory such as `tmp/` to see if the behavior is the same? `Library` subdirectories are for any files you don’t want exposed to the user. – l'L'l Apr 28 '17 at 22:54
  • Yes. Same behavior on /DocumentDirectory and /tmp/ – JoakimE Apr 28 '17 at 23:09
  • Interesting. The only other suggestion I might have at the moment would be to possibly try and recreate a new image from the one that is a no show. I've run into similar problems before on osx and worked out after doing it that way. – l'L'l Apr 28 '17 at 23:13
  • Naming `path` a URL object is misleading. The error is here `path.absoluteString`. If you don't rename it you need to pass the url path property `path.path`. If you rename it to url just use `url.path` – Leo Dabus Apr 29 '17 at 00:19
  • And use it with the UIImage contentsOfFile initializer `let image = UIImage(contentsOfFile: url.path)` – Leo Dabus Apr 29 '17 at 00:44
  • 1
    That works @LeoDabus ! Thanks! – JoakimE Apr 29 '17 at 09:43

0 Answers0