0

I'm trying to populate a CollectionView with images stored on a Parse.com account. I wrote a simple query to get these images and store them in an array. I just don't know how to go about putting those images in the cells.

class AvatarCollectionViewController: UICollectionViewController {

    var avatars: [PFObject] = []

    var user: PFUser!

    var imageFile: PFFile

    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Get the avatars from Parse
        let avatarQuery = PFQuery(className: "Avatar")
        avatarQuery.findObjectsInBackgroundWithBlock{
            (objects: [PFObject]?, error: NSError?) -> Void in

            if error == nil {
                if let objects = objects {
                    for object in objects {
                        self.avatars.append(object)
                        self.imageFile = object["file"] as! PFFile
                    }
                }
            }
        }
    }

Further down I have the function that handles the cell. I know I'm getting close but I just can't get the images to show:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! AvatarViewCell

    let userImageFile = self.avatars[indexPath.row]["file"]

    print(userImageFile)

    userImageFile?.getDataInBackgroundWithBlock{
        (imageData: NSData?, error: NSError?) -> Void in
        if error == nil{

            let image = UIImage(data: imageData!)

            cell.imageCell.image = image

        } else {

            print(error)
        }
    }


    //cell.imageCell.image = UIImage(named: self.avatars[indexPath.row]["file"] as! String)

    return cell
}

The whole view is black, no errors. Any help is appreciated. Thanks

EDIT: Updated cellForItemAtIndexPath function. Nil error crash at cell.imageCell.image = image, even though print(userImageFile) prints a value to the console.

winston
  • 3,000
  • 11
  • 44
  • 75
  • Looks like you try to use PFFile as an image. As far as I know PFFile is not an image, but it contains an url which can be used to download image data. Also, you'll probably need to use UIImage(data:) to set downloaded image – Nimble Feb 20 '16 at 03:55
  • Ok, I think you're right. Do I have the initial setup right? How do I now take the image data out of the array elements within the collection view? I thought maybe I could do `if let object = objects` within the collection view function but I'm worried that I will slow the app down – winston Feb 20 '16 at 04:02
  • And you are right about the PFFile. I just looked it up in the Parse dashboard. It seems to just be a link to the image. – winston Feb 20 '16 at 04:03
  • At the first glance your initial setup is good (not sure if you need self.imageFile at all). As an option you can let AvatarViewCell to care about downloaded image. Put an url to a cell and download an image in this cell. By the way, you can use thirdparty frameworks to set remote images https://github.com/rs/SDWebImage or implement it on your own http://stackoverflow.com/questions/24231680/loading-image-from-url – Nimble Feb 20 '16 at 04:07
  • I think I'm missing something (sorry, first time using collection view). XCode isn't even hitting my breakpoints in the cellForItemAtIndexPath function. Is there a reloadData function I need to place in the ViewDidLoad? Thanks! – winston Feb 20 '16 at 04:31
  • yes, you need to call `collectionView.reloadData()` when data is downloaded and ready to be used – Nimble Feb 20 '16 at 04:38
  • Thanks! Almost there. Updated cellForItemAtIndexPath function in my original question. Nil error crash at `cell.imageCell.image = image`, even though `print(userImageFile)` prints a value to the console. – winston Feb 20 '16 at 04:46
  • You must make sure that data is not nil in "let image = UIImage(data: imageData!)". Do not force-unwrap it, use "if let". – almas Feb 20 '16 at 05:54
  • `if let image = UIImage(data: imageData){` still forces me to force-unwrap it. But why would it be nil in the first place? userImageFile isn't nil. Is there something wrong with `let userImageFile = self.avatars[indexPath.row]["file"]` ? – winston Feb 20 '16 at 13:34

0 Answers0