Im storing an NSURL
for a Facebook profile picture that looks like "http://graph.facebook.com/(id)/picture?type=large" where id is the user's id.
I want to display the image from that url in a UIImage
I am trying to do this like so:
var data = NSData(contentsOfURL: profilePictureURL)
cell.backgroundImage = UIImage(data: data)
But XCode throws an error on that second line that says:
"Missing argument for parameter 'inBundle' in call" but obviously that's not a parameter in this call. I even tried adding it once before and once after like UIImage(data: data, inBundle: nil) and reversed but it said "extra argument inBundle"
Help please!
**** EDIT ****
Found the problem:
cell.backgroundImage = UIImage(data: data)
should be
cell.backgroundImage.image = UIImage(data: data)
As you note, you want to set the image
property of cell.backgroundImage
. Also, you will want to unwrap that optional.
You can do a forced unwrapping:
let data = NSData(contentsOfURL: profilePictureURL)
cell.backgroundImage.image = UIImage(data: data!)
Or, a little safer, and optional binding:
if let data = NSData(contentsOfURL: profilePictureURL) {
cell.backgroundImage.image = UIImage(data: data)
}
And, if you want to be especially prudent, and do this asynchronously, it would look like:
cell.backgroundImage.image = nil // initialize it to nil before we go to get image asynchronously
NSURLSession.sharedSession().dataTaskWithURL(profilePictureURL) { data, response, error in
if data == nil {
println("dataTaskWithURL error: \(error)")
} else {
if let image = UIImage(data: data) {
dispatch_async(dispatch_get_main_queue()) {
cell.backgroundImage.image = image
}
}
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments