简体   繁体   中英

How can I display UIImage array in cell?

I have a UIImage array populated with PFFiles from Parse.

how do I put the UIImages into the cell?

在此处输入图片说明

Below is my code.

var arrayOfFriends = [UIImage]()

    var imagequery = PFQuery(className: "_User")
    query.findObjectsInBackgroundWithBlock {( objects: [AnyObject]?, error: NSError?) -> Void in
        for object in objects!{
            let userPic = object["ProPic"] as! PFFile
            userPic.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
                if(error == nil){
                    let image = UIImage(data: imageData!)
                    self.arrayOfFriends.append(image!)
                    print(self.arrayOfFriends)
                }
                dispatch_async(dispatch_get_main_queue()) {
                    self.collectionView.reloadData()
                }
            })

        }
    }
 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: friendcellView = collectionView.dequeueReusableCellWithReuseIdentifier("friendcell", forIndexPath: indexPath) as! friendcellView


    //this is where the error occurs(error message above)
    cell.friendpic.image = UIImage(named: arrayOfFriends[indexPath.row])
    cell.friendpic.layer.cornerRadius = cell.friendpic.frame.size.width/2;
    cell.friendpic.clipsToBounds = true


    return cell
}

You already have it:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: friendcellView = collectionView.dequeueReusableCellWithReuseIdentifier("friendcell", forIndexPath: indexPath) as! friendcellView


    //this is where the error occurs(error message above)
    // change this line
    // cell.friendpic.image = UIImage(named: arrayOfFriends[indexPath.row])
    cell.friendpic.image = arrayOfFriends[indexPath.row] // here, arrayOfFriends[indexPath.row] is a UIImage, you don't have to do anything more
    cell.friendpic.layer.cornerRadius = cell.friendpic.frame.size.width/2;
    cell.friendpic.clipsToBounds = true


    return cell
}

Just change this line

cell.friendpic.image = UIImage(named: arrayOfFriends[indexPath.row])

to this:

cell.friendpic.image = arrayOfFriends[indexPath.row]
   arrayOfFriends[indexPath.row].getDataInBackgroundWithBlock { (data: NSData?, erreur: NSError?) -> Void in
        if erreur == nil {
            let image = UIImage(data: data)
            cell.friendpic.image = image
        }
    }

this goes in cellForRowAtIndexPath

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM