2016-11-16 22 views
6

Önbellek dizinindeki bir haritanın anlık görüntüsünü kaydetmeye çalışıyorum ve var olduğunda onu geri almaya çalışıyorum. Ancak, oluşturulan dosyaya rağmen, UIImage (contentOfFile :), onu almaya çalıştığımda nil verir. Hem yazma hem de okuma için dosya yollarını yazdırdım ve bunlar aynıdır ve kabı indirip dizini kontrol ederek ve dosyayı kesinlikle mevcutsa dosyayı doğrulamışlardır.UIImage (contentOfFile :) önbellek dizininde bulunan dosyaya rağmen geri dönüyor

Burada sorun nedir?

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'' olmalıdır atPath: self.mapCachesDirectory.path' absoluteString ve yol özellikleri arasındaki farkın absoluteString bu durumda, url şeması içerdiğini olduğunu –

+0

"// dosyası" Dosyayı bulamadığı için yolunun olması gerekiyordu, ama aslında bu mutlak değerdir. –

+1

@LeoDabus Mükemmel olan problemdi. Bir cevap eklemek isterseniz, çözülen soruyu işaretleyebilirim. Teşekkürler! – doovers

cevap

13

Sorun, yol özelliğini kullanmanız gereken URL özelliği mututeString kullandığınız var. absoluteString and yol özellikleri arasındaki fark, absoluteString dosyanın url şemasını ("file: //") içerdiğidir; bu, dosyanın yolunda olması beklenen dosyada bulunmamasının nedeni, ancak aslında mutlakString'idir. olduğu

İlgili konular