ios - 如何在单元格中显示 UIImage 数组?

标签 ios iphone swift parse-platform

我有一个 UIImage 数组,其中填充了 Parse 中的 PFFiles。

如何将 UIImages 放入单元格中?

enter image description here

下面是我的代码。

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
}

最佳答案

你已经拥有了:

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
}

关于ios - 如何在单元格中显示 UIImage 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33338703/

相关文章:

ios - UIView autoresizingMask 在设置不同的固定左右边距和灵活宽度时出现错误?

ios - 索引还是标签?对于多个复选框作为 IBOutletCollection 中的按钮

ios - UNUserNotificationCenter didReceive/willPresent 未触发 ios 11

ios - 调整音量 ios

ios - 为什么按钮宽度在 UINavigationbar 中有冲突?

ios - 不小心删除了我的应用程序启动屏幕

iPhone - PushMeBaby 示例错误

ios - 成员(member)中心无法添加设备

iPhone:当 UITableView 滚动到 60 多个自定义单元格时应用程序崩溃

ios - 如何在 UIImageView (Swift) 中显示来自 Parse 的图像?