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

六眼飞鱼酱① 提交于 2019-11-26 00:38:33

问题


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

    DispatchQueue.global(qos: .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.stopAnimating()
            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()

    DispatchQueue.global(qos: .userInitiated).async {
        if let compositeImage = compositeImage, let info = self.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 {
                log.error(error)
            }
        }
    }
}

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)
    mapView.add(overlay)

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

回答1:


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.



来源:https://stackoverflow.com/questions/40642217/uiimagecontentsoffile-returning-nil-despite-file-existing-in-caches-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!