我的 SDWebImage 存在问题,我有一个 UICollectionView
,并且我正在尝试将 URL 图像获取到 indexPath
。我被困住了,似乎我错过了牢房中的一个关键点。对此有任何帮助。我想这是一件愚蠢的事情,但是嘿..还要提一下我有很多 URL 图片而不仅仅是一个
这是我的结构
struct Regions {
let imageName: String
let imageUrl: String
let titleName: String
let action: String
let actionUrl: String
init(imageName: String, imageUrl: String, titleName: String, action: String, actionUrl: String) {
self.imageName = imageName
self.imageUrl = imageUrl
self.titleName = titleName
self.action = action
self.actionUrl = actionUrl
}
}
我的附加方法
actions.append(Regions(imageName: "agorianicam", imageUrl: "http://agorianicam.forecastweather.gr/agorianicam.jpg", titleName: "Αγόριανη", action: "agoriani", actionUrl: "http://5.196.92.163:8080/agoriani.html"))
cellForItemAtIndexPath:
方法
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! RegionListingCollectionCell
// cell.imgView.sd_setImageWithURL.image = UIImage.init(named: actions[indexPath.row].imageUrl)
cell.imgView.image = UIImage.init(named: actions[indexPath.row].imageName)
cell.titleLabel?.text = actions[indexPath.row].titleName
return cell
}
最佳答案
使用cell.imgView.sd_setImage(with: yourUrl, placeholderImage: anyPlaceholderImage)
而不是分配图像。或者参见SDWebImage Doc了解更多信息。
因此,根据您的代码,下面的代码应该适合您:
cell.imgView.sd_setImage(with: URL(string: actions[indexPath.row].imageUrl), placeholderImage: anyPlaceholderImage)
关于swift - SDWebImage,在indexPath.row中获取url图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50715290/