Agung
Agung

Reputation: 13803

error while catching an image from an URL

I am trying to catch an image from a URL in my localhost, but somehow it gives an error

    import UIKit

let path = "http://localhost/Twitter/Post/52/POST-BCFE0D62-CE3F-407B-B510-CC3B9A8372CF.jpeg"

let url = URL(string: path)!

do {
    let imageData = try Data(contentsOf: url)
    let image = UIImage(data: imageData)
} catch {
    print("error \(error)")
}

here is the result in my playground. what went wrong in here :(

enter image description here

error message :

error Error Domain=NSCocoaErrorDomain Code=256 "The file “POST-BCFE0D62-CE3F-407B-B510-CC3B9A8372CF.jpeg” couldn’t be opened." UserInfo={NSURL=http://localhost/Twitter/Post/52/POST-BCFE0D62-CE3F-407B-B510-CC3B9A8372CF.jpeg}

Upvotes: 0

Views: 263

Answers (1)

Dilip Tiwari
Dilip Tiwari

Reputation: 1451

Swift 3.0

let url = URL(string: "http://i.imgur.com/w5rkSIj.jpg")
let data = try? Data(contentsOf: url)

if let imageData = data {
    let image = UIImage(data: data)
}

Use above code for getting image from url.Hope it will help you.

Upvotes: 1

Related Questions