-1

I've a string in which I've comma separated links of images. Here is how I'm splitting it into an array: let imagesLinks = imageLins.components(separatedBy: ","). Then I've used for loop to get one link, download the image and storing it in a UIImage array in this way:

for imag in imagesLinks
        {
            let img = UIImageView()

            print("\(baseReportImageURL)\(imag)")

            img.sd_setImage(with: URL(string: "\(baseReportImageURL)\(imag)"), placeholderImage: nil)
            imagesArray.append(img.image!)
        }

The print statement is giving me the correct URL which when I open on browser downloads the image. The problem is on the line where I'm appending the array i.e. imagesArray.append(img.image!). I get:

fatal error: unexpectedly found nil while unwrapping an Optional value

and

fatal error: unexpectedly found nil while unwrapping an Optional value

So what would be the correct solution for this?

UPDATE

My question is different because I'm using SDWebImage and when I use completion block there is a strange behaviour of the app:

img.sd_setImage(with: imgURL, placeholderImage: nil,options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
                activityIndicator.stopAnimating()
                imagesArray.append(image!)
                self.photoCollection.reloadData()
            })

So it keeps on rotating the activity indicator and when I go back and push the view again it load the images instantly. So I think that the completion block is not called when the image is downloaded but why is that?

Chaudhry Talha
  • 7,231
  • 11
  • 67
  • 116

4 Answers4

1

I think that is not the proper way to get downloaded images (set images to an UIImageView and then get the images from there).

You should use the image downloader, provided by SDWebImage:

SDWebImageManager.shared().imageDownloader?.downloadImage(with: <YOUR_URL>, options: [], progress: { (received, expected, nil) in
            print(received,expected)
        }, completed: { (image, data, error, true) in
            yourArray.append(image)
        })

If you have indexes, you can update the current row in the tableview every time when an image downloaded, or just reload() the whole, but I recommend the first.

Dániel Grimm
  • 157
  • 1
  • 10
0

It is because

  imagesArray.append(img.image!)

Image take some times for downloading. When you do imagesArray.append(img.image!) at that time it is possible that your image is not set to your imageview and that means you are trying to add nil in your imageArray!

Second thing why are you storing image in an array ? You have array of urls and you are using SDWebImage then every time when you want to display image use SDWebImage. No need to store in array!

And if you want images in array anyhow than use NSUrlSession asynchronous requests with completion handlers and from completion handler add image to your array!

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • I've a comma separated string with image URL's and SDWebImage completion block is not working for me somehow. Kindly see if you can add to what I've added in the update section of my question. – Chaudhry Talha Oct 17 '17 at 07:39
0

Append the image into the images array in the sd_setImage completion block. The image is not yet downloaded when you are trying to add it into the array, and check if the image is not equal nil before adding it.

Ayman Ibrahim
  • 1,359
  • 15
  • 24
0

Use this Block For Image Download After download Perform Operation(append Image)

cell.appIcon.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
 // Perform operation.
//append Image Here
}) 
Bhumesh Purohit
  • 491
  • 1
  • 8
  • 26