简体   繁体   English

Swift-从URL下载图像后如何调整UICollectionViewCells的大小?

[英]Swift - How to resize UICollectionViewCells after image is downloaded from URL?

I am working on an iOS app that has a collection view. 我正在使用具有收藏夹视图的iOS应用程序。 The size of the collection view cells (instagram photos) are configured to be perfect squares, but the client does not want cropped images, so I am trying to create a completion handler in the cell class which will send a call back to a method defined in my view controller through a delegate which will send back a CGSize I can work with. 集合视图单元格(instagram照片)的大小被配置为完美的正方形,但是客户端不希望裁剪图像,因此我试图在单元格类中创建一个完成处理程序,该处理程序将调用返回给定义的方法在我的视图控制器中,通过一个委托将其发送回我可以使用的CGSize。

The code uses the Kingfisher Library to download the images 该代码使用Kingfisher库下载图像

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
}
}

I checked out the definition for this line: 我检查了这一行的定义:

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

and found that it takes in a completion handler: 并发现它包含一个完成处理程序:

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

How do I define a custom completion handler in the InstagramPostCell class to use the delegate to send the CGSize of the newly downloaded image back to the View Controller? 如何在InstagramPostCell类中定义自定义完成处理程序,以使用委托将新下载的图像的CGSize发送回View Controller?

I hope that makes sense 我希望这是有道理的

Thanks, T 谢谢,T

found a solution 找到了解决方案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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