1

I am trying to set the image property on a UIImageView. When I use a UIImage to set the .image property it throws this error every time:

"unexpectedly found nil while unwrapping an Optional value"

The problem is that my UIImage is not nil.

Here is the code where I am setting the UIImage

func setPhotosForNewsItem(photoArray:[Photo]) {
    println("Image Count: " + String(photoArray.count))
    var image:UIImage = photoArray[0].photo
    println(image.description)
    self.newsImage.image = image
}

Here is the console output:

Image Count: 2 UIImage: 0x7fdd93c5cdd0, {1115, 1115} fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

I am able to use the Quick Look tool in xCode on my supposedly nil UIImage and see the photo that I am trying to use. Why would I be throwing a nil error when my UIImage is clearly not nil?

UPDATE::

It seems that I am not properly storing the UIImage in my array. Here is where I download my images and store them to my array for unpacking later.

var relatedPhotos:[PFObject] = relations as! [PFObject]
                            //println(relations!)
                            var photoArray:[Photo] = [Photo]()

                            for photo in relatedPhotos {
                                var newPhoto = Photo()
                                var photoFile:PFFile = photo.objectForKey("photo") as! PFFile
                                newPhoto.object = photo
                                newPhoto.objectID = photo.objectId!
    photoFile.getDataInBackgroundWithBlock({(imageData: NSData?, error: NSError?) -> Void in
                                        if (error == nil) {

                                            newPhoto.photo = UIImage(data:imageData!)!
                                            //println(newPhoto.photo)
                                            photoArray.append(newPhoto)

                                            if photoArray.count == relatedPhotos.count {

                                                if newObject is FieldReport {
                                                    var report = newObject as! FieldReport
                                                    report.photos = photoArray
                                                    updatedReports.append(report)
                                                    //println("Report Count 1: " + String(updatedReports.count))
                                                }
                                                else {
                                                    var report = newObject as! Feature
                                                    report.photos = photoArray
                                                    updatedReports.append(report)
                                                }

                                                if updatedReports.count == objects.count {


                                                    self.delegate?.fieldReports(updatedReports)
                                                }
                                            }

                                        }
                                    })
}

I know that this works to download and display the photo as I have just used it successfully to do so. To me that means I am not storing the UIImage properly. Is there a different way I should be storing these image files?

Tim Larsen
  • 13
  • 1
  • 4
  • Update: By changing self.newsImage.image = image to self.newsImage?.image = image I have been able to silence the error, but it still is not setting my image. – Tim Larsen Jul 02 '15 at 16:52
  • how is 'photo' property declared on your class? did you try also self.newsImage.image = image! ? and also - see http://stackoverflow.com/a/26675638/2382237 – Doro Jul 02 '15 at 17:34
  • var photo : UIImage? – Tim Larsen Jul 02 '15 at 17:59

1 Answers1

0

You can prevent the crash from happening by safely unwrapping

func setPhotosForNewsItem(photoArray:[Photo]) {
    println("Image Count: " + String(photoArray.count))
   if  var image:UIImage = photoArray[0].photo
   {
       println(image.description)
       self.newsImage.image = image
    }
}

Setup breakpoint and check what's wrong

EDIT:

The only thing i can purpose - check what happens here:

newPhoto.photo = UIImage(data:imageData!)!

seems like it's the main reason of all problems. check type of imageData, try to convert it to image via, for example UIImage(CGImage: <#CGImage!#>). You need to figure out how to deal with this image.

Doro
  • 2,413
  • 2
  • 14
  • 26
  • Thanks Doro, will do. I'm still figuring out this wrapping and unwrapping in swift. It's starting to make sense. Any idea why my UIImage is showing nil even though it's not nil? – Tim Larsen Jul 02 '15 at 17:04
  • seems like it's not properly stored in array. try to log .photo prop and init manually self.newsImage.image = UIImage(named: "logged_photo_name") – Doro Jul 02 '15 at 17:07
  • When I print the .photo property I just get - UIImage: 0x7fdd93c5cdd0, {1115, 1115} - not the photo name. My understanding is that UIImage doesn't store the name once it has been set. Is there some way to print out the photo name? – Tim Larsen Jul 02 '15 at 17:18
  • you can do the next trick - save photo to disk, and then pick it again using uiimage constructor http://iostechsolutions.blogspot.com/2014/11/swift-save-and-load-image-from.html – Doro Jul 02 '15 at 17:22
  • Just updated my question. Seems you are correct that I'm not storing the UIImage in my array properly, but I'm not sure why that would be. Mind taking a look at the updated code? – Tim Larsen Jul 02 '15 at 17:45
  • @TimLarsen, i've edited my question. that's all i can say, you need to debug your method to know the truth =) – Doro Jul 03 '15 at 05:50
  • Thanks! You were right, that line was the problem. Since I'm pulling the file from Parse, I just changed the point where I downloaded it from the server and saved the pointer to the photo instead of the photo itself. Problem solved. – Tim Larsen Jul 03 '15 at 18:47