尽管文件存在于缓存目录中,UIImage(contentsOfFile :)返回nil
我正在尝试在缓存目录中保存带有覆盖的地图快照,并在存在时检索它。但是,尽管创建了文件,但当我尝试检索它时,UIImage(contentsOfFile :)返回nil。我已经打印了写入和读取的文件路径,它们是相同的,并通过下载容器并检查目录并且文件确实存在来验证文件是否存在。
知道这里的问题是什么吗?
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()
jeck猫
临摹微笑