gpt4 book ai didi

ios - UITableViewCell 图片从 URL 下载

转载 作者:行者123 更新时间:2023-11-28 12:52:35 24 4
gpt4 key购买 nike

我正在使用 alamofire 从 URL 下载图像。我在点击时收到图片,但在显示 View 时却没有。
如何在出现 View 时获取所有图像?这是我用来实现该目标的代码:

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)
}

}
}

}

我应该添加除此之外的任何内容吗?

最佳答案

因为dispatch_async是异步传输,下载图片后要刷新tableview,但是我看你用的是SDWebImage,那你就不需要asycloadp函数了,换个代码再试试。

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

let imageUrl:NSURL? = NSURL(string:url)
cell.imageView!.sd_setImageWithURL(imageUrl, completed:{(image: UIImage?, error: NSError?, cacheType: SDImageCacheType!, imageURL: NSURL?) in
if (image != nil) {
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
}
})

return cell
}

}

关于ios - UITableViewCell 图片从 URL 下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36006719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com