0

I'm using UIImageView.image in order to change the visible image on my screen.

iv.image = images[index]

The array 'images' is currently filled with local image files. However, I wish to download images from my server and then append them to the array.

private var images = [img1, img2, img3]

I have been recommended using SDWebImage (particularly SDWebImageManager or SDWebImageDownloader) to do this, however, when exploring the download and caching tutorials, all of them downloaded to a UIImageView. I cannot pass in a UIImageView into the .image extension. I couldn't find any tutorials or examples to help me achieve this. I am fairly new to swift so I do not have a vast amount of experience or understanding.

  • https://stackoverflow.com/a/37019507/3400991 use this – Shobhakar Tiwari Jun 01 '17 at 19:01
  • I have an extension like that in my code already, however, when It only applies to UIImageView's when I need to apply it to a UIImage array. Unless my understanding of what's going on is wrong, I'm not sure how I would make this work – Aidan McVay Jun 01 '17 at 19:09

1 Answers1

0

You can donwload image Async without any library as well.

Following will create a extension for imageView and it will async down image from the server just pass the respective url as parameter it will return an image.

extension UIImageView {
  public func imageFromServerURL(urlString: String) {
    URLSession.shared.dataTask(with: NSURL(string: urlString)! as URL, completionHandler: { (data, response, error) -> Void in
        if error != nil {
            print(error!)
            return
        }
        DispatchQueue.main.async(execute: { () -> Void in
            if let imageData = data {
                let image = UIImage(data: imageData)
                self.image = image
            }
        })            
    }).resume()
  }
}

// And you can call like this where ever required

cell.cellImageView.imageFromServerURL(urlString: banner.imageURL)
Subrat Padhi
  • 128
  • 11
  • I need to return multiple images and put them into an array though. It's no good having an extension for imageView for me since the way I'm changing images is by altering the index of the array and using `imageView.image = UIImageArray[index]` – Aidan McVay Jun 02 '17 at 11:35