Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialmixforce
9,747 PointsDoes anyone know how to show an image from URL with Swift?
I am trying to show an external Image from an URL.
2 Answers
Daniel Sattler
4,867 PointsThere are several ways to get this done.
Async:
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
var myImage = UIImage(data: NSData(contentsOfURL: NSURL(string:"http://upload.wikimedia.org/wikipedia/en/4/43/Apple_Swift_Logo.png")))
})
or, as i prefer, a cached async version:
Create a new Class or a new iOS Swift File with this content:
class ImageLoader {
var cache = NSCache()
class var sharedLoader : ImageLoader {
struct Static {
static let instance : ImageLoader = ImageLoader()
}
return Static.instance
}
func imageForUrl(urlString: String, completionHandler:(image: UIImage?, url: String) -> ()) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {()in
var data: NSData? = self.cache.objectForKey(urlString) as? NSData
if let goodData = data {
let image = UIImage(data: goodData)
dispatch_async(dispatch_get_main_queue(), {() in
completionHandler(image: image, url: urlString)
})
return
}
var downloadTask: NSURLSessionDataTask = NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: urlString)!, completionHandler: {(data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
if (error != nil) {
completionHandler(image: nil, url: urlString)
return
}
if data != nil {
let image = UIImage(data: data)
self.cache.setObject(data, forKey: urlString)
dispatch_async(dispatch_get_main_queue(), {() in
completionHandler(image: image, url: urlString)
})
return
}
})
downloadTask.resume()
})
}
}
and in your actual viewController file use this:
ImageLoader.sharedLoader.imageForUrl("http://upload.wikimedia.org/wikipedia/en/4/43/Apple_Swift_Logo.png", completionHandler:{(image: UIImage?, url: String) in
self.myImage.image = image!
})
i hope this helps.
Daniel Sattler
4,867 PointsYou are very welcome ;-)
mixforce
9,747 Pointsif possible, could you please help me out of this question as well?
https://teamtreehouse.com/forum/importing-a-json-that-contains-imageurl-text
Thanks Daniel.
mixforce
9,747 Pointsmixforce
9,747 PointsThat works! Thanks a lot!
nagendra rao
Courses Plus Student 56 Pointsnagendra rao
Courses Plus Student 56 PointsThe app freezes if the url doesn't return an image! "http://upload.wikimedia.org/wikipedia/en/4/43/Apple_Swift_Logo.png" no longer exists and the app breaks, how do I handle the 404 image links in this code?
nagendra rao
Courses Plus Student 56 Pointsnagendra rao
Courses Plus Student 56 PointsGot it!
This is what is creating problem when the link is not found, below I have corrected the code,
Notice that I have removed the ! after "image"