doovers doovers - 1 year ago 107
Swift Question

UIImage(contentsOfFile:) returning nil despite file existing in caches directory

I'm trying to save a snapshot of a map with an overlay in the caches directory and retrieve it when it exists. However, despite the file being created, UIImage(contentsOfFile:) returns nil when I try to retrieve it. I've printed the file paths for both write and read and they are the same and have verified the file exists by downloading the container and checking the directory and the file definitely exists.

Any idea what the problem here is?

let cachesDirectory: URL = {
let urls = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)
return urls[urls.endIndex - 1]

let mapCachesDirectory = cachesDirectory.appendingPathComponent("map-snapshots", isDirectory: true)

func configureMap(data: NSSet?) {
mapView.isZoomEnabled = false
mapView.isScrollEnabled = false
mapView.isUserInteractionEnabled = false

guard let data = data as? Set<SessionData>, data.count > 0 else { return }

activityIndicatorView.isHidden = false
activityIndicatorView.startAnimating() .userInitiated).async {
var points = [CLLocationCoordinate2D]()
for object in data {
guard object.locationLatitude != 0, object.locationLatitude != 0 else { continue }
points.append(CLLocationCoordinate2DMake(object.locationLatitude, object.locationLongitude))
DispatchQueue.main.async(execute: {
self.createOverlay(points: points)
self.activityIndicatorView.isHidden = true
self.cacheMapImage(view: self.mapView)

func cacheMapImage(view: UIView) {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, true, 0)
view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
let compositeImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext() .userInitiated).async {
if let compositeImage = compositeImage, let info = {
let data = UIImagePNGRepresentation(compositeImage)
do {
var isDirectory: ObjCBool = false
let fileManager = FileManager.default
if fileManager.fileExists(atPath: self.mapCachesDirectory.absoluteString, isDirectory: &isDirectory) == false {
try fileManager.createDirectory(at: self.mapCachesDirectory, withIntermediateDirectories: true, attributes: nil)
let fileURL = self.mapCachesDirectory.appendingPathComponent(info.uid).appendingPathExtension("png")
try data?.write(to: fileURL)
print("\(fileURL.absoluteString) Saved")
} catch {

func cachedMapImage() -> UIImage? {
guard let info = info else { return nil }
let filePath = mapCachesDirectory.appendingPathComponent(info.uid).appendingPathExtension("png").absoluteString
let image = UIImage(contentsOfFile: filePath)
print("\(filePath): \(image)")
return image

func createOverlay(points: [CLLocationCoordinate2D]) {
guard points.count > 0 else { return }

let overlay = MKGeodesicPolyline(coordinates: points, count: points.count)

let inset: CGFloat = 50.0
mapView.setVisibleMapRect(overlay.boundingMapRect, edgePadding: UIEdgeInsetsMake(inset,inset,inset,inset), animated: true)

Answer Source

The problem there is that you are using URL property absoluteString where you should be using the path property. The difference between absoluteString and path properties is that absoluteString includes the file url scheme ("file://") which is the reason it doesn't find the file at what was supposed to be its path but it is actually its absoluteString.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download