Skip to content

Instantly share code, notes, and snippets.

@y16ra
Created February 3, 2015 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y16ra/2c694ef0fd26d2edce88 to your computer and use it in GitHub Desktop.
Save y16ra/2c694ef0fd26d2edce88 to your computer and use it in GitHub Desktop.
iOSでTwitterのアイコンを取得して表示する
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
println("AccountsViewController#tableView cellForRowAtIndexPath called.")
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
// ユーザIDを表示する
cell.textLabel?.text = accounts[indexPath.row].username
// プロフィール画像を取得して非同期で表示させる
var q_global: dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
var q_main: dispatch_queue_t = dispatch_get_main_queue();
cell.imageView?.image = nil;
dispatch_async(q_global, {
var ac: AnyObject = self.accounts[indexPath.row]
let userid = ac.valueForKeyPath("properties.user_id")! as String
let requestAPI = NSURL(string: "https://api.twitter.com/1.1/users/show.json")
println("twitter userid=\(userid)")
let req = SLRequest(forServiceType: SLServiceTypeTwitter,
requestMethod: SLRequestMethod.GET,
URL: requestAPI,
parameters: ["user_id": userid])
req.account = ac as ACAccount
let slHandler: SLRequestHandler = {(response, urlResponse, error) in
if let jsonObj: AnyObject = NSJSONSerialization.JSONObjectWithData(response, options: NSJSONReadingOptions.MutableLeaves, error: nil) {
var responseJson = jsonObj as NSDictionary
let profImgUrl = responseJson["profile_image_url"] as String
var imageData: NSData? = NSData(contentsOfURL: NSURL(string: profImgUrl)!)
if var image: UIImage = UIImage(data: imageData!) {
dispatch_async(q_main, {
cell.imageView?.image = image;
cell.layoutSubviews()
});
}
self.prof[userid] = profImgUrl
} else {
println("jsonArray is nil")
}
}
req.performRequestWithHandler(slHandler)
})
return cell;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment