3
votes

I'm successfully pulling in data from Parse into swift, but my images don't seem to be working the way I'm doing it.

In my cellForRowAtIndexPath method, I do the following:

        var event: AnyObject? = eventContainerArray[indexPath.row]

        if let unwrappedEvent: AnyObject = event {
            let eventTitle = unwrappedEvent["title"] as? String
            let eventDate = unwrappedEvent["date"] as? String
            let eventDescription = unwrappedEvent["description"] as String
            let eventImage = unwrappedEvent["image"] as? UIImage

            println(eventImage)

            if (eventImage != nil){


                cell.loadItem(date: eventDate!, title: eventTitle!, description: eventDescription, image: eventImage!)}


            else {


                let testImage: UIImage = UIImage(named: "test-image.png")!

                cell.loadItem(date: eventDate!, title: eventTitle!, description: eventDescription, image: testImage )


            }

        }
    }

    return cell
}

I'm using println() with my PFQuery, and I am seeing this as part of the object that's loading in: image = "<PFFile: 0x7fee62420b00>";

So I'm getting title, date, description, etc. all loading fine as part of the above eventContainerArray, but when I look at eventImage, it's nil every time. In the code above, it always defaults to loading test-image.png, being that the image is coming up nil. Am I simply handling that PFFile the improper way? Not sure why it's not working.

Thanks!

2

2 Answers

4
votes

Your image is likely a PFFile object. You will need to load it to get an UIImage object from it.

let userImageFile = unwrappedEvent["image"] as PFFile
userImageFile.getDataInBackgroundWithBlock {
    (imageData: NSData!, error: NSError!) -> Void in
    if error == nil {
        let eventImage = UIImage(data:imageData)
        // do something with image here
    }
}
1
votes

I'm not familiar with Parse, but given what you're seeing from the println() it looks like what you've received isn't a UIImage but instead some sort of data type. This makes sense; you aren't going to be receiving ObjC objects over the network. So you are getting a file, but when you use the conditional downcast (x as? UIImage) it's returning nil because you don't have a UIImage.

Instead you're going to need to cast to PFFile, and then probably create a UIImage with UIImage(data: file.getData) or something similar. I'm not familiar with exactly how Parse works.

Edit: here's a related question that might be helpful: I can't get image from PFFile