我想寫公共函數加載圖像異步並返回UIImage。我想用它來填充圖像的UITableView。公共函數加載圖像異步並返回UIImage
class CommonFunctions {
func loadImageAsync(StringURL: NSString) -> UIImage{
var returningImage = UIImage()
let url = NSURL(string: StringURL)
let requestedURL = NSURLRequest(URL: url!)
NSURLConnection.sendAsynchronousRequest(requestedURL, queue: NSOperationQueue.mainQueue(), completionHandler: {
response, data, error in
if error != nil {
println("there is some error loading image")
}
else {
if let image = UIImage(data: data){
returningImage = image
}
}
})
return returningImage
}
}
的問題是,當我要使用此功能:
cell.imageModelImage.image = CommonFunctions.loadImageAsync(CommonFunctions)
而是字符串參數我得到的類?爲什麼?
爲什麼你將CommonFunctions作爲參數傳遞給loadImageAsyn()?爲什麼你將實例方法稱爲類方法? – Abdullah 2015-04-06 10:32:35