我使用alamofire从URL下载图像。 我正在获取图片onclick,但不是视图出现时。
如何在视图出现时获取所有图像?这是我使用来实现,代码:从URL下载UITableIViewCell图像
import UIKit
import Alamofire
import SwiftyJSON
import SDWebImage
import Kingfisher
class Paragogoi: UITableViewController {
var data=[FronItem]()
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request(.GET, "http://gaiaskarpos.com/getCategores.php").validate().responseJSON { response in
switch response.result {
case .Success:
if let value = response.result.value
{
let json = JSON(value)
let list: Array<JSON> = json.arrayValue
var urls=[String]()
for element in list{
let id:String=element["id"].stringValue
let name:String=element["name"].stringValue
let url:String=element["url"].stringValue
self.data.append(FronItem(id:id,name:name,url:url))
};
self.tableView.reloadData()
}
case .Failure(let error):
print(error)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("paragogosCell", forIndexPath: indexPath)
cell.textLabel?.text = data[indexPath.row].name
asycloadp(data[indexPath.row].url!, imageView: cell.imageView!)
return cell
}
func asycloadp(url:String,imageView:UIImageView){
let downloadQueue=dispatch_queue_create("myqueue", nil)
print(url)
dispatch_async(downloadQueue){
let imageUrl:NSURL?
= NSURL(string:url)
dispatch_async(dispatch_get_main_queue()){
imageView.sd_setImageWithURL(imageUrl)
}
}
}
}
我要补充这比其他东西吗?
是的,我尝试这种情况,而是当点击细胞获取图像。 –
@adhslock我再次更新了代码,这个应该可以工作,我已经测试过了。 – BobGao
是的,它工作。感谢您的帮助。 –