首页 > 解决方案 > Swift - 从 URL 下载图像后如何调整 UICollectionViewCells 的大小?

问题描述

我正在开发一个具有集合视图的 iOS 应用程序。集合视图单元格(instagram 照片)的大小被配置为完美的正方形,但客户端不想要裁剪的图像,所以我试图在单元格类中创建一个完成处理程序,它将回调发送回定义的方法在我的视图控制器中,通过委托将发回我可以使用的 CGSize。

该代码使用翠鸟库下载图像

import UIKit
import Kingfisher


class InstagramPostCell: UICollectionViewCell {

@IBOutlet weak var imageView: UIImageView!
var delegate: CellResizeDelegate?

func setupForInstagramPost(_ post: InstagramPost, delegate: CellResizeDelegate) {
    self.backgroundColor = UIColor.clear
    self.imageView.kf_setImage(with: URL(string:post.imageURL)!)
    self.imageView.clipsToBounds = true
    self.imageView.contentMode = .scaleAspectFill
    self.imageView.backgroundColor = UIColor.clear
    self.imageView.tintColor = UIColor.clear
    self.delegate = delegate
}
}

我检查了这一行的定义:

self.imageView.kf_setImage(with: URL(string:post.imageURL)!)

并发现它需要一个完成处理程序:

  public func kf_setImage(with resource: Resource?, placeholder: Image? = default, options: KingfisherOptionsInfo? = default, progressBlock: Kingfisher.DownloadProgressBlock? = default, completionHandler: Kingfisher.CompletionHandler? = default) -> Kingfisher.RetrieveImageTask

如何在 InstagramPostCell 类中定义自定义完成处理程序以使用委托将新下载图像的 CGSize 发送回视图控制器?

我希望这是有道理的

谢谢,

标签: iosswift

解决方案


找到了解决办法

self.imageView.kf_setImage(with: URL(string:post.imageURL)!, completionHandler:{
        (image, error, cacheType, imageUrl) in
    })

推荐阅读