盡管文件存在于緩存目錄中,UIImage(contentsOfFile :)返回nil 我正在嘗試在緩存目錄中保存帶有覆蓋的地圖快照,并在存在時檢索它。但是,盡管創(chuàng)建了文件,但當我嘗試檢索它時,UIImage(contentsOfFile :)返回nil。我已經(jīng)打印了寫入和讀取的文件路徑,它們是相同的,并通過下載容器并檢查目錄并且文件確實存在來驗證文件是否存在。知道這里的問題是什么嗎?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()
2 回答

jeck貓
TA貢獻1909條經(jīng)驗 獲得超7個贊
問題是您正在使用URL屬性absoluteString,您應(yīng)該使用path屬性。absoluteString
和path
屬性之間的區(qū)別在于absoluteString包含文件url方案(“file://”),這是它沒有找到應(yīng)該是它的路徑的文件但它實際上是它的absoluteString的原因。

臨摹微笑
TA貢獻1982條經(jīng)驗 獲得超2個贊
atPath: self.mapCachesDirectory.absoluteString
應(yīng)該是atPath: self.mapCachesDirectory.path
- 2 回答
- 0 關(guān)注
- 1791 瀏覽
添加回答
舉報
0/150
提交
取消