2016-11-16 4 views
6

キャッシュディレクトリにオーバーレイがあるマップのスナップショットを保存し、それが存在するときに取得しようとしています。しかし、ファイルの作成にもかかわらず、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() 

    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」は、「atPath:self.mapCachesDirectory.path」にする必要があります。 –

+0

absoluteStringとpathのプロパティの違いは、absoluteStringにURLスキームが含まれていることです。この場合は「file://」です。ファイルが見つからない理由はそのパスであるはずでしたが、実際にはabsoluteStringです –

+1

@LeoDabus優れていることが問題でした。回答を追加したい場合は、解決された質問に印を付けることができます。ありがとう! – doovers

答えて

13

問題は、pathプロパティを使用する必要があるURLプロパティabsoluteStringを使用していることです。 absoluteString andのパスプロパティの違いは、absoluteStringにファイルのURLスキーム( "file://")が含まれていることです。これはパスと思われていた場所でファイルが見つからない理由ですが、実際はabsoluteStringです。

関連する問題