问题描述
我正在尝试在缓存目录中保存带有叠加层的地图快照,并在它存在时检索它.然而,尽管文件被创建,当我尝试检索它时 UIImage(contentsOfFile:) 返回 nil.我已经打印了写入和读取的文件路径,它们是相同的,并且通过下载容器并检查目录和文件确实存在来验证文件存在.
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)
}
推荐答案
问题在于您在应该使用 path 属性的地方使用了 URL 属性 absoluteString.absoluteString
和 path
属性之间的区别在于 absoluteString 包含文件 url 方案(file://"),这就是它在什么位置找不到文件的原因应该是它的路径,但它实际上是它的绝对字符串.
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.
这篇关于尽管缓存目录中存在文件,但 UIImage(contentsOfFile:) 返回 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!