swift - SDWebImage,在indexPath.row中获取url图像

标签 swift uicollectionview sdwebimage

我的 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/

相关文章:

arrays - 警告 "Simultaneous accesses to parameter ' self' ..."真的适用于此吗?

ios - Swift 中的分割操作

ios - UICollectionView重新加载数据问题

ios - AVCaptureSession 与 UIImagePickerController 速度

IOS Swift 表格 View : Is there a way to move first cell one row down

ios - 查看 UICollectionViewCell 的调试?

ios - 使用 SDWebImage 下载的图像的 UIImageView 幻灯片

ios - UITableViewCell 中的图像 url 异步但按顺序加载

ios - 使用 SDWebImage block 的 ImageView 上的圆形进度条

ios - Collection View in TableView Show item [VGS.CustumCell collectionView :numberOfItemsInSection:]: unrecognized selector sent to instance