Search code examples
iosswiftkingfisher

Load URL into array using Kingfisher


I've a button where I can upload images to my app. And once I upload them they are stored into an array which contains URLs. Now I'm trying to retrieve the image from the URL and insert it into an imageview, and right after it will be inserted into an UIImage array. But it's always returns NIL.

First off, I'm getting the error, cuz it's an array:

Cannot convert value of type '[String]' to expected argument type 'String'

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

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

    }

So it crashes at the "imgArray.append" since imageView returns NIL.

downloadURL DOES have an URL so it's not nil.


Solution

  • Since you want to download all images, you have to do something like this:

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