swift - 如何从 URL 快速加载图像

标签 swift url uiimage iso

我正在从 firebase 传递一个字符串格式的 URL 以加载图像,该 url 传递正常但 cell.articleImage.sd_setImage(with: url, placeholderImage: UiImage(named: "issaimage")) 没有返回来自 url 的图片,只是占位符图片

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "articleCell") as? articlesCell else {
            return UITableViewCell()}

        let article = articleArray[indexPath.row]
        let url = URL(string: article.imageURL)!

        cell.articleImage.sd_setImage(with: url, placeholderImage: UIImage(named: "issaimage"))

        cell.configureCell(title: article.ArticleTitle, author: article.author, date: article.date)

        return cell
    }

最佳答案

This code isn't allowing me to use the image variable

let image = UIImage(data: imageData)

因为它是局部变量,所以如果您希望它在类内的任何地方都可以访问,那么它必须是一个实例变量

也不要使用 Data(contentsOf: url) 因为它会阻塞主线程,在后台队列中下载它然后在主队列中显示它或者简单地使用 SDWebImage pod安装后使用

let url = URL(string: article.imageURL)!
cell.imageView.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder.png"))

关于swift - 如何从 URL 快速加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51460680/

相关文章:

ios - 在响应式(Reactive)编程中创建静态流是个坏主意吗?

swift - 更改 GameViewController 中必须首先执行的场景后出错

magento - 重写 Magento session 的最后一个 URL

JAVA获取URL API

ios - 如何在 UIView 中屏蔽 UIImage View ?

ios - 用于在图像上叠加文本和图形的图形库和函数

swift - EXC_BAD_INSTRUCTION 等待 Task.sleep

arrays - 检查我的数组是否包含字典

javascript - 如何使用 Express.js 处理空 URL 参数

ios - 为 UIImage 添加白色边框