将图像高度从图像 URL 设置为 PinterestLayout CollectionView

时间:2021-03-05 20:51:38

标签: ios swift url uicollectionview

我用 PinterestLayout 创建了一个 CollectionView,所以这是一个具有不同单元格高度的集合视图。这是可行的,但是当我使用 Firestore 将我的代码放入项目时(当我从 URL 获取单元格中的图像时)我遇到了问题,因为在这一行“let photo = post.floverImg”中,我想我必须输入类似的内容单元格“cell.imageView.sd_setImage(with: URL(string: flovers[indexPath.row].floverImg!))”显示图像而不是字符串。

扩展 HomeViewController { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection 部分: Int) -> Int { 返回 flovers.count }

     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
        cell.imageView.sd_setImage(with: URL(string: flovers[indexPath.row].floverImg!))
        cell.label.text = flovers[indexPath.row].floverName
      //  cell.descriptionLabel.text = flovers[indexPath.row].floverName
        cell.layer.cornerRadius = 15
        
        
    
        
        
       
        return cell
    }
}




func collectionView(collectionView: UICollectionView, heightForPhotoAt indexPath: IndexPath, with width: CGFloat) -> CGFloat
{
    
    if let post = posts?[indexPath.item], let photo = post.floverImg {
        let boundingRect = CGRect(x: 0, y: 0, width: width, height: CGFloat(MAXFLOAT))
        let rect = AVMakeRect(aspectRatio: photo.size(), insideRect: boundingRect)
       
        return rect.size.height
    }
    
    return 200
}
 

https://ibb.co/J3KdDtG https://ibb.co/JzXZv3V

0 个答案:

没有答案
相关问题