gpt4 book ai didi

ios - 我无法在 UICollectionView 中显示图像

转载 作者:行者123 更新时间:2023-11-28 05:53:21 24 4
gpt4 key购买 nike

我想在 collectionView 中显示 UIImage

从 Json 数据中获取图像路径。

图片路径是从之前赋值的变量中获取的我正在尝试显示图像,但无法获取。

API 正在运行。

但我试图输出它来检查变量的值 var photo = [Stores.photos]? 但它没有显示在控制台上

此外,如果您直接应用 imageURL,而不是从变量中应用,图像将被显示。如何为 var photo = [Stores.photos] 赋值?

StorePhotoCollectionView

class StorePhotoViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {

var store_id = ""

var store : [Store]?
var photo : [Store.photos]?

@IBOutlet weak var mainImage: UIImageView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var locationLabel: UILabel!
@IBOutlet weak var storePhotoUIView: UIView!


override func viewDidLoad() {
super.viewDidLoad()

//Request API
let url = URL(string: "http://localhost:8000/store/api?store_id=" + store_id)
let request = URLRequest(url: url!)
let session = URLSession.shared

let encoder: JSONEncoder = JSONEncoder()
encoder.dateEncodingStrategy = .iso8601
encoder.outputFormatting = .prettyPrinted

session.dataTask(with: request){(data, response, error)in if error == nil,
let data = data,
let response = response as? HTTPURLResponse{

let decoder: JSONDecoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
do {

let json = try decoder.decode(Store.self, from: data)


self.store = [json]
self.photo = json.photos

DispatchQueue.main.async {
self.nameLabel.text = json.name
self.locationLabel.text = json.location
}

} catch {
print("error:", error.localizedDescription)

}

}

}.resume()

}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

//UIView Corner redius
let uiViewPath = UIBezierPath(roundedRect: storePhotoUIView.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 8, height: 8))
let uiViewMask = CAShapeLayer()
uiViewMask.path = uiViewPath.cgPath
storePhotoUIView.layer.mask = uiViewMask
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return photo?.count ?? 0
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


let imageCell : UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath)
let imageView = imageCell.contentView.viewWithTag(1) as! UIImageView

print("asssss")

let imageURL = URL(string: "http://127.0.0.1:8000/photos/" + photo![indexPath.row].path)
if imageURL == nil {
print("nil")
}else{
DispatchQueue.global().async {
let data = try? Data(contentsOf: imageURL!)
if let data = data{
let image = UIImage(data: data)
DispatchQueue.main.async {
imageView.image = image
}
}
}
}
return imageCell
}

}

最佳答案

你忘了刷新你的 Collection View ,最初为你的 Collection View 创建对象

@IBOutlet weak var yourcollectionView: UICollectionView!

辅助你的主线程 UI 更新,刷新 UICollectionView

DispatchQueue.main.async {
self.nameLabel.text = json.name
self.locationLabel.text = json.location
yourcollectionView.reloadData()

}

最后从服务器加载图像,在这里你正在执行主线程调用,它会卡住你的应用程序,图像显示过程参见 this example

关于ios - 我无法在 UICollectionView 中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52144185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com