2016-11-16 12 views
6

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) 
} 
+1

'atPath: self.mapCachesDirectory.absoluteString' phải là' atPath: self.mapCachesDirectory.path' –

+0

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 –

+1

@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

Trả lời

13

Vấn đề ở đây là bạn đang sử dụng thuộc tính URL absoluteString nơi bạn nên sử dụng thuộc tính đường dẫn. Sự khác biệt giữa các thuộc tính đường dẫn absoluteString and là absoluteString bao gồm lược đồ url của tệp ("tệp: //"), đó là lý do nó không tìm thấy tệp ở những gì được cho là đường dẫn của nó nhưng thực ra nó là absoluteString của nó.

Các vấn đề liên quan