ios - 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 发送回视图控制器?
我希望这是有道理的
谢谢,
解决方案
找到了解决办法
self.imageView.kf_setImage(with: URL(string:post.imageURL)!, completionHandler:{
(image, error, cacheType, imageUrl) in
})
推荐阅读
- apache-dolphinscheduler - dev 分支删除了 install.sh 脚本?
- sql - 如何从两列中提取数据,因为我缺少 regexp_match(..) 之类的函数
- selenium - 在 testng.xml 中运行为 testng 套装触发 IE 浏览器并手动运行每个测试,它使用 chrome 运行。为什么这样?
- django - 如何只允许Django UpdateView中文章的作者访问文章更新页面?
- r - 如何将 CSV 文件读入在文件中使用两种分隔符的 R 中?
- qt - 无法使用 QSettings 保存设置
- flutter - 如何在 Flutter 的列表中搜索一个值?
- search - 根据 Key 搜索... split(Act) = 286. split[Act] = 286
- java - 如何使用 Jackson 将 JSON 字符串序列化为 JAVA 对象
- ruby-on-rails - ruby on rails中的NameError