ios - 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 的选项?
谢谢!
解决方案
推荐阅读
- android-studio - `flutter packages get` 和 `flutter pub get` 有什么区别?
- python - 如何在 tkinter 中将对象保持在其他对象之上
- python - python中的数据框创建问题
- netlogo - 如何在 Netlogo 中更改浅色品种的颜色
- algorithm - 高效的分治算法
- html - 如何将导航与最多 3 个元素(列表)连续居中 [响应式]
- postgresql - 为什么在安装过程中加密密钥在 Postgresql TDE 中不起作用?“从 encryption_key_command 收到的未知数据”
- firebase-authentication - 如何在 Okta 中使用 Firebase 身份验证?
- google-analytics - 针对本地 Web 应用程序的分析工具推荐
- android-studio - 如何在 Android Studio 中获取远程 GitHub URL?