簡體   English   中英

Swift-從URL下載圖像后如何調整UICollectionViewCells的大小?

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

我正在使用具有收藏夾視圖的iOS應用程序。 集合視圖單元格(instagram照片)的大小被配置為完美的正方形,但是客戶端不希望裁剪圖像,因此我試圖在單元格類中創建一個完成處理程序,該處理程序將調用返回給定義的方法在我的視圖控制器中,通過一個委托將其發送回我可以使用的CGSize。

該代碼使用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
}
}

我檢查了這一行的定義:

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發送回View Controller?

我希望這是有道理的

謝謝,T

找到了解決方案

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