Search code examples
iosimagecachingnsdatawritetofile

iOS Write Data to file, FILE DOES NOT EXIST


I'm tryin to write the datas of an Image in the cacheDirectory, like this :

let imageData = UIImageJPEGRepresentation(image, 1.0) else { fatalError("Impossible to read the image") }
try! imageData.write(to: cachedImageURL, options: .atomic)
print(fileManager.fileExists(atPath: cachedImageURL.absoluteString))

Where image is a working UIImage. the Print statement always returns False. The file is never written, and any exception is raised due to the "try!"

Any idea ?

printing the cachedImageURL, it looks like this

file:///var/mobile/Containers/Data/Application/7B6B72C8-E794-4474-A658-48B66AEA31EC/Library/Caches/79d0e60e-5961-4538-bd9f-28187f4e26d0.jpg


Solution

  • Just figured out. The problem come from my print

    print(fileManager.fileExists(atPath: cachedImageURL.absoluteString))
    

    which should be cachedImageURL.path insteadOf cachedImageURL.absoluteString

    print(fileManager.fileExists(atPath: cachedImageURL.path))