首页 > 解决方案 > Swift 视图未显示 UIImage,尽管它从 API 打印出值

问题描述

我一直在排除故障以显示 UIImageView,我不知道它在哪里不让我。

主视图控制器,

class MainViewController: UIViewController {
    
    @IBOutlet weak var mainTableView: UITableView!
    
    var imageArray : [PhotoModel] = []
    var images = [UIImage]()
    var imageRatio = [CGFloat]()
    var currentIndex = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.hidesBackButton = true

        
        let dispatchGroup = DispatchGroup()
        dispatchGroup.enter()
        
        PhotoManager().fetchPhotos(query: "") { (data, error) in
            self.imageArray = data
            
            for d in data {
                let url = URL(string: d.url)
                let imageURL = try? Data(contentsOf: url!)
                let image = UIImage(data: imageURL!)
                self.images.append(image!)
            }
            
            for d in data {
                let ratio = CGFloat(d.width / d.height)
                self.imageRatio.append(ratio)
            }
            dispatchGroup.leave()
        }
        dispatchGroup.notify(queue: .main){
            print("reloadingData")
            self.mainTableView.reloadData()
        }
    }
        
}
extension MainViewController: UITableViewDataSource, UITableViewDelegate {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return imageArray.count
    }
      
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      
        let cell = tableView.dequeueReusableCell(withIdentifier: "imageCell", for: indexPath) as! ImageTableViewCell
        DispatchQueue.main.async {
            cell.randomImageView?.image = self.images[indexPath.row]
            cell.nameLabel?.text = self.imageArray[indexPath.row].username
        }
        return cell
        
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return tableView.frame.width/imageRatio[indexPath.row]
    }
    
    //tableView delegate
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        currentIndex = indexPath.row
        print(imageArray[currentIndex].name)
        performSegue(withIdentifier: "full", sender: self)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destination = segue.destination as! FullViewController
        destination.images = self.images
        destination.currentIndex = self.currentIndex
    }
}

全视图控制器,

class FullViewController: UIViewController {

    @IBOutlet weak var fullCollectionView: UICollectionView!
    
    var images = [UIImage]()
    var currentIndex = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        fullCollectionView.isPagingEnabled = true
    }
    
    override func viewWillAppear(_ animated: Bool) {
        fullCollectionView.selectItem(at: IndexPath(item: currentIndex, section: 0), animated: true, scrollPosition: .centeredHorizontally)
    }
}

extension FullViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! FullImageCollectionViewCell
        cell.fullScreenImages.image = images[indexPath.row]
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
}

ImageTableViewCell,

class ImageTableViewCell: UITableViewCell {

 
    @IBOutlet weak var randomImageView: UIImageView?
    @IBOutlet weak var nameLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }
}

当我打印出 imageArray 时,它会打印出 PhotoModel 数组和图像 <UIImage:0x600001e641b0 anonymous {1080, 1080}> 以及所有间距,标签似乎都有效。

有没有显示 UIImageview 的选项?

谢谢!

标签: iosswiftmodel-view-controlleruiimageviewstoryboard

解决方案


推荐阅读