BeniaminoBaggins
BeniaminoBaggins

Reputation: 12433

Retrieve Image from Parse in Swift

I am trying to retrieve a PFObject from Parse which has a class name (type) of Sticker, that has a property of type File called imageFile, that holds a .png image. I am then trying to set an existing UIImageView to have that image from parse as its UIImage.

Here is my code. Please scroll to the right to see my code comment:

import UIKit
import Parse

class DressingRoomViewController:   UIViewController,
                                    UICollectionViewDelegateFlowLayout,
                                    UICollectionViewDataSource {

    @IBOutlet weak var MirrorImageView: UIImageView!
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var heightConstraint: NSLayoutConstraint!
    let identifier = "cellIdentifier"
    let dataSource = DataSource()
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    let cellSpacing: CGFloat = 2
    let cellsPerRow: CGFloat = 6
    let numberOfItems = 12

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.dataSource = self



        var query = PFQuery(className:"Sticker")
        query.getObjectInBackgroundWithId("WGYIYs0crU") { 
            (sticker: PFObject?, error: NSError?) -> Void in
            if error == nil && sticker != nil {
                println(sticker)
                if let stickerImage = sticker!.objectForKey("imageFile") as? NSData { // it goes from this line, straight to the bottom of the function, without going into the if statement or even the else statement.
                    let file = PFFile(name:"resume.txt", data:stickerImage)




                    file.getDataInBackgroundWithBlock {
                        (imageData: NSData?, error: NSError?) -> Void in
                        if (error == nil) {
                            if let imageData = imageData {
                                let image = UIImage(data:imageData)
                                self.MirrorImageView.image = image
                            }
                        } else {
                            println(error)
                        }
                    }
                }
            } else {
                println(error)
            }
        }
    }

Why does the code not run past the code comment?

EDIT: Just realised I didn't have an else statement for that if statement. I put one in, and it goes into the else statement. How do I get execution to go into the if statement?

Upvotes: 0

Views: 115

Answers (1)

ConfusedByCode
ConfusedByCode

Reputation: 1730

Since it's skipping the if block, sticker!.objectForKey("imageFile") as? NSData must be nil. It might be nil for a few possible reasons. First, make sure your Sticker class actually has an "imageFile" property. Second, make sure object "WGYIYs0crU" actually has data saved in that property. You can easily check those things by logging into the parse web console.

However, I suspect the problem is that you are trying to downcast to NSData. Files are usually saved on Parse as PFFile, so try casting to PFFile, and skip the line where you create a new PFFile. Something like this:

var query = PFQuery(className:"Sticker")

query.getObjectInBackgroundWithId("WGYIYs0crU") {
    (sticker: PFObject?, error: NSError?) -> Void in

    // Also, checking for nil isn't really necessary, since that's what if let does.
    if let stickerImage = sticker?["imageFile"] as? PFFile {
        file.getDataInBackgroundWithBlock {
            (imageData: NSData?, error: NSError?) -> Void in

            if let imageData = imageData {
                let image = UIImage(data:imageData)
                self.MirrorImageView.image = image
            }

            if let downloadError = error {
                println(downloadError.localizedDescription)
            }
        }
    }

    if let imageError = error {
        println(imageError.localizedDescription)
    }
}

Hope that helps.

Upvotes: 1

Related Questions