Created
February 3, 2015 23:17
-
-
Save y16ra/2c694ef0fd26d2edce88 to your computer and use it in GitHub Desktop.
iOSでTwitterのアイコンを取得して表示する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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