首页 > 解决方案 > 应用程序上的图像太小,而且有很多空白单元格,我怎样才能让我的图像变大,以便它可以容纳我手机屏幕的一半?

问题描述

显示时图像很小,如何将其缩小到手机大小的一半?

import UIKit
import Kingfisher

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet var tableview: UITableView!

    var arrayOfImages = ["https://firebasestorage.googleapis.com/v0/b/starlaxcy.appspot.com/o/myimage.png?alt=media&token=adcfa5ce-5bc3-4a65-bdcd-fb190f4c38d2"]

    override func viewDidLoad() {
        self.tableview.register(ImageCell.self, forCellReuseIdentifier: "ImageCell")
        super.viewDidLoad()
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrayOfImages.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ImageCell") as! ImageCell

        let resource = ImageResource(downloadURL: URL(string: arrayOfImages[indexPath.row])!, cacheKey: arrayOfImages[indexPath.row])

        cell.imageView?.kf.setImage(with: resource)

        return cell
    }
}

标签: iosswiftuitableviewkingfisher

解决方案


如果您有自定义单元格ImageCell

let cell = tableView.dequeueReusableCell(withIdentifier: "ImageCell") as! ImageCell

不要imageView设置超类的内置属性UITableViewCell

cell.imageView?.kf.setImage(with: resource)

而是创建一个 imageView ,在该类的 init 方法中添加适当的约束并将其与 kingfisher 一起使用


推荐阅读