加载swift后返回图像

时间:2015-04-11 11:57:32

标签: swift uiimage nsurl

我有以下功能:

func getPlayerImgById(playerId: String) -> UIImage {
    var sourceKey = "MY_KEY"
    var playerSummaries = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?steamids=\(playerId)&key=\(sourceKey)"
    var playersImg = UIImage(named: "no_avatar")
    Request.get(playerSummaries, done: { (error: NSError?, data: NSData, jsonResult: NSDictionary?) -> () in
        let response = jsonResult!["response"] as! NSDictionary
        let jsonSummaries = response["players"] as! NSArray
        if jsonSummaries[0].count > 0 {
            var playersImgUrl: String = jsonSummaries[0]["avatar"] as! String!

            dispatch_async(dispatch_get_main_queue(), {
                playersImg = UIImage(data: NSData(contentsOfURL: NSURL(string: playersImgUrl)!)!)
                println(playersImgUrl)
            })
        }
    })

    return playersImg!
}

但是这个函数总是返回默认图像。我该如何解决?

- 已编辑

我使用以下请求类:https://gist.github.com/frozzare/cfb79749e56b5abb00c7

0 个答案:

没有答案