Tôi đang cố lưu ảnh chụp nhanh bản đồ với lớp phủ trong thư mục bộ nhớ cache và truy xuất nó khi nó tồn tại. Tuy nhiên, mặc dù các tập tin được tạo ra, UIImage (contentsOfFile :) trả về nil khi tôi cố gắng lấy nó. Tôi đã in các đường dẫn tệp cho cả việc ghi và đọc và chúng giống nhau và đã xác minh tệp tồn tại bằng cách tải xuống vùng chứa và kiểm tra thư mục và tệp chắc chắn tồn tại.UIImage (contentsOfFile :) trở về nil bất chấp tệp hiện có trong thư mục lưu trữ
Bất kỳ ý tưởng nào về sự cố ở đây?
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)
}
'atPath: self.mapCachesDirectory.absoluteString' phải là' atPath: self.mapCachesDirectory.path' –
sự khác biệt giữa absoluteString và thuộc tính đường dẫn là absoluteString bao gồm lược đồ url, trong trường hợp này là "file: //" lý do nó không tìm thấy các tập tin tại được cho là con đường của nó, nhưng nó thực sự là tuyệt đối của nó tuyệt đối –
@LeoDabus tuyệt vời đó là vấn đề. Nếu bạn muốn thêm câu trả lời, tôi có thể đánh dấu câu hỏi đã được giải quyết. Cảm ơn! – doovers