-1

Multiple images when downaloded asynchronously and added to array for further shown in different tabs of views

var bgImage = UIImage()
        if let checkedUrl = NSURL(string: state.bckImageUrl as String) {

            if !state.bgImageDownloaded {

                getDataFromUrl(checkedUrl) { (data, response, error)  in
                    dispatch_async(dispatch_get_main_queue()) { () -> Void in
                        guard let datum = data where error == nil else { return }
                        self.bgView.image = UIImage(data: datum)
                        bgImage = self.bgView.image!    //EXcePtion BAD INSTRUCTION
                        self.bckgrndImages.append(bgImage)
                        state.bgImageDownloaded = true
                    }
                }
            }else{
                self.bgView.image = bckgrndImages[currentStory]
            }
        }
Sanjay
  • 83
  • 2
  • 11

1 Answers1

1

Before the line of error, print:

bgView
bgView.image

One of those is nil

If bgView is nil, then your outlet from the Main Storyboard connection may be broken. Recobnect it.

If 'bgView.image' is nil, then there is no image in the imageView.

Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61