-1

I have post response i want to download image from image_path

let fileUrl = NSURL(fileURLWithPath: (posts.value(forKey: "image_path") as! [String])[indexPath.row])
        print(fileUrl as Any) // here i can get path

        if FileManager.default.fileExists(atPath: (fileUrl)// nil value {
            let url = NSURL(string: (posts.value(forKey: "image_path") as! [String])[indexPath.row]) // Here url found nil
            let data = NSData(contentsOf: url! as URL)
            cell.LocationImage?.image = UIImage(data: data! as Data)
        }

UPDATE:

[1]: https://i.stack.imgur.com/KWCS9.png

ViralRathod
  • 324
  • 1
  • 3
  • 12
  • check out my answer with afnetworking 3.0 https://stackoverflow.com/questions/36007238/afnetworking-3-0-cant-download-image/39695800#39695800 or you can use sd webimage for downloading image. If your url is external from server – Jitendra Modi Jun 05 '17 at 12:03
  • @ViralRathod your image_path is URL in post response?? – Tushar Sharma Jun 05 '17 at 12:03
  • @TusharSharma posts is my response from soap, 'posts.value(forKey: "image_path") as! [String])[indexPath.row]' – ViralRathod Jun 05 '17 at 12:06
  • @ViralRathod i mean what is the value for key "image_path".Is it a URL?? – Tushar Sharma Jun 05 '17 at 12:07
  • @TusharSharma its url string path http:/sf.iipl.info/ImageCSharp.aspx%3FLocation=C:%5C%5Cinfinityhost%5C%5Cdemo11.iipl.info%5C%5Cdata%5C%5Capp%5C%5Cuser_location_photo%5C%5C8000034939_Resize%5C%5C&FileName=1_35f28e55-ca31-4918-8b41-9eb7f3070ace -- file:/// – ViralRathod Jun 05 '17 at 12:09
  • This is my response string == http://sf.iipl.info/ImageCSharp.aspx?Location=C:\\infinityhost\\demo11.iipl.info\\data\\app\\user_location_photo\\8000034939_Resize\\&FileName=1_35f28e55-ca31-4918-8b41-9eb7f3070ace – ViralRathod Jun 05 '17 at 12:11
  • 1
    Possible duplicate of [How to show image from the imagePath in swift3](https://stackoverflow.com/questions/44363372/how-to-show-image-from-the-imagepath-in-swift3) – Scriptable Jun 05 '17 at 12:58
  • Why are you using nsurl? Use URL with swift and you need to cast. – Mozahler Jun 05 '17 at 14:21
  • Oops. You won't need to cast. – Mozahler Jun 05 '17 at 20:35

1 Answers1

0

That URL is not a local file path URL, nor a valid URL accoridng to my browser.

The URL you have provided above returns a server error in the browser and does not return an image. See screenshot

Screenshot

You would need to ensure that the image is accessible and the URL actually returns an image response firstly. Then you would need to download the image. Not sure if you are using any libraries or not so I will post an example without.

//: Playground - noun: a place where people can play

import UIKit
import XCPlayground
import PlaygroundSupport

let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

// random image from images.google.com
let urlString = "https://files.allaboutbirds.net/wp-content/uploads/2015/06/prow-featured-240x135.jpg"

let url = URL(string: urlString)
let session = URLSession.shared

let task = session.dataTask(with: url!) { data, response, error in
    guard error == nil else {
        print("[ERROR] - Failed to download image")
        return
    }

    if let data = data {
        let image = UIImage(data: data)
        DispatchQueue.main.async {
            imageView.image = image
        }
    }
}

let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
view.addSubview(imageView)

task.resume()
PlaygroundPage.current.liveView = view

UPDATE:

Screenshot 2

Scriptable
  • 19,402
  • 5
  • 56
  • 72
  • This is my response URL "http://sf.iipl.info/ImageCSharp.aspx?Location=C:\\infinityhost\\demo11.iipl.info\\data\\app\\user_location_photo\\8000034939_Resize\\&FileName=1_35f28e55-ca31-4918-8b41-9eb7f3070ace" – ViralRathod Jun 05 '17 at 12:46
  • try it in browser... does it show an image or a server error? – Scriptable Jun 05 '17 at 12:46
  • when you paste in browser it directly Download image and that image i can open in preview – ViralRathod Jun 05 '17 at 12:48
  • see my updated answer. The image is on a remote server, a File URL is supposed to be for a local resource as far as I am aware. Ensure that the URL works and try my code above. see if it works – Scriptable Jun 05 '17 at 12:48
  • please see this https://stackoverflow.com/questions/44327312/sort-my-tableviewcell-with-distance-comes-from-response-in-swift3 and https://stackoverflow.com/questions/44363372/how-to-show-image-from-the-imagepath-in-swift3 please if you can help more – ViralRathod Jun 05 '17 at 12:55
  • You shouldn't duplicate questions. I've updated my answer. I have the image showing with the URL provided in your second link above. – Scriptable Jun 05 '17 at 12:59
  • please see my screenshot – ViralRathod Jun 05 '17 at 13:20
  • above the line starting `let task = ` add `print(url)`. The only thing that could be nil there is the URL but I have the exact same code and its fine for me?. or preferably use a breakpoint to see what the value is – Scriptable Jun 05 '17 at 13:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145879/discussion-between-viralrathod-and-scriptable). – ViralRathod Jun 05 '17 at 13:24
  • found nil my friend value is nil also – ViralRathod Jun 05 '17 at 13:27