繁体   English   中英

使用Kingfisher将URL加载到数组中

[英]Load URL into array using Kingfisher

我有一个按钮,可以将图像上传到我的应用程序。 一旦我上传它们,它们就会存储到包含URL的数组中。 现在,我尝试从URL检索图像并将其插入到imageview中,然后将其插入UIImage数组中。 但是它总是返回NIL。

首先,我得到了错误,因为它是一个数组:

无法将类型“ [String]”的值转换为预期的参数类型“ String”

    if downloadURL != []
    {
        let url = URL(string: downloadURL) //ERROR HERE

        imageView.kf.setImage(with: url)
        imgArray.append(imageView.image!)

    }

由于imageView返回NIL,因此它在“ imgArray.append”处崩溃。

downloadURL有URL,因此不是nil。

由于要下载所有图像,因此必须执行以下操作:

for url in downloadURL {
    guard let imageURL = URL(string: url) else {
        continue 
    }
    imageView.kf.setImage(with: imageURL)
    // to load images separately use this
    KingfisherManager.shared.retrieveImage(with: imageURL) { result in
         let image = try? result.get().image
         if let image = image {
              imgArray.append(image)
         }
     }
}

您正在尝试使用array初始化URL ,需要使用String初始化URL (错误清楚地告诉您)

另外, kf.setImage(with:)需要初始化的URL ,而不是String ,因此:

尝试这样的事情:

if downloadURL.count > 0 {
        if let urlString = downloadURL.last { // I intuit that the url you want is the last one you appended 

        let url = URL(string: urlString)
        imageView.kf.setImage(with: url)

        if let image = imageView.image {
          imgArray.append(image) // Only append if the image was set conrrectly
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM