Skip to content

Instantly share code, notes, and snippets.

@sigwyg
Created February 24, 2016 06:44
Show Gist options
  • Save sigwyg/77b6a9cd8e60a4b0a9c2 to your computer and use it in GitHub Desktop.
Save sigwyg/77b6a9cd8e60a4b0a9c2 to your computer and use it in GitHub Desktop.
Swiftで画像を非同期で取得するサンプル。Table View Cell内で同機種得すると、すげー重くなるので
// set icon-image
let imageView = cell.viewWithTag(1) as! UIImageView
let imgPath = object.valueForKey("imgPath")!.description as String
if imgPath.isEmpty {
imageView.image = UIImage(named: "no_img.png")
}
else {
let url = NSURL(string: imgPath)
let requestUrl = NSURLRequest(URL: url!)
NSURLConnection.sendAsynchronousRequest(requestUrl, queue: NSOperationQueue.mainQueue()) { (response, data, error) -> Void in
if error != nil {
print(error)
}
else {
if let dlImage = UIImage(data: data!) {
imageView.image = dlImage
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment