Swift mapview cannot display thumbnail image

Viewed 128

I am writing image to a file and reading back inside mapview didSelect method and it gives image data read error.

 var localImageFileUrl: URL? = nil

            do {
                delegate.documentDirectoryUrl = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)

                if let data = aStruct.imageData  {
                    localImageFileUrl =  delegate.documentDirectoryUrl!.appendingPathComponent("Image.png")
                    UserDefaults.standard.set(localImageFileUrl, forKey: "localImageFileUrl")
                    try data.write(to: localImageFileUrl!)
                }

            }
            catch {
                print("error")
            }

Test here it works fine.

          do{
                 print("imageUrl0: \(localImageFileUrl)")
                let imageData: Data = try Data(contentsOf: localImageFileUrl as! URL)
                let image = UIImage(data: imageData)
               print(image)
            }catch{
                print("Unable to load image data: \(error)")
            }

debug window output :

imageUrl0: Optional(file:///var/mobile/Containers/Data/Application/19A5A226-3CAF-4DC0-9015-40278703C5BA/Documents/Image.png)

Optional(, {120, 120}) works fine.

I pass data via a struct like this:

   let endPtPin = GpsData.MyEndPt(latitude:  Double(latitudeStr) as! Double, longitude: Double(longitudeStr) as! Double, url: localImageFileUrl! as NSURL)

Inside mapview didSelect method, I extract url, and try to access the image, but it gives error:

 func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

     DispatchQueue.main.async {
        if let thumbnailImageButton = view.leftCalloutAccessoryView as? UIButton,
        let url = (view.annotation as? GpsData.MyEndPt)?.thumbnailUrl
         {
            print("imageUrl: \(url)")
            do{
                let imageData: Data = try Data(contentsOf: url as URL)
                let image = UIImage(data: imageData)
                    thumbnailImageButton.setImage(image, for: UIControlState.normal)

            }catch{
                print("Unable to load image data: \(error)")
            }
        }
    }
}

debug Window gives error:

imageUrl: Optional(file:///var/mobile/Containers/Data/Application/19A5A226-3CAF-4DC0-9015-40278703C5BA/Documents/Image.png) 2020-03-27 00:07:04.079576-0500 r2nr[9945:4518812] NSURLConnection finished with error - code -1002

2020-03-27 00:07:07.009134-0500 r2nr[9945:4518751] CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme

Unable to load image data: Error Domain=NSCocoaErrorDomain Code=256 "The file “Image.png)” couldn’t be opened." UserInfo={NSURL=Optional(file:///var/mobile/Containers/Data/Application/19A5A226-3CAF-4DC0-9015-40278703C5BA/Documents/Image.png)}

The filepath has not changed but cannot read the image data from file. What is wrong?

1 Answers

I spent couple of hours and found the way to work it. I was accessing the image data as Data

if let data = aStruct.imageData  {
      localImageFileUrl =  delegate.documentDirectoryUrl!.appendingPathComponent("Image.png")
      UserDefaults.standard.set(localImageFileUrl, forKey: "localImageFileUrl")
      try data.write(to: localImageFileUrl!)
 }

I cast the data to PNG format like this and it worked fine:

  do {
        delegate.documentDirectoryUrl = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
       let image = UIImage(data: aContactStruct.imageData!)

       if let data = UIImagePNGRepresentation(image!){{
             localImageFileUrl =  delegate.documentDirectoryUrl!.appendingPathComponent("Image.png")
              UserDefaults.standard.set(localImageFileUrl, forKey: "localImageFileUrl")
              try data.write(to: localImageFileUrl!)
       }

   }
   catch {
            print("error")
   }
Related