Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading image in Swift from Parse

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!

like image 642
hudsonian Avatar asked Dec 06 '25 08:12

hudsonian


1 Answers

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
    }
}
like image 68
picciano Avatar answered Dec 07 '25 21:12

picciano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!