【发布时间】:2020-01-22 17:05:05
【问题描述】:
我想知道如何将 API 图像从 API 显示到 UIImage。我可以从控制台获取结果,但无法将图像从 Internet 显示到集合视图。
public var foodImage = [String]()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MainPageCollectionViewCell
//Below is fine and it can display food title from Internet
cell.FoodTitle.text = self.foodTitle[indexPath.item]
//Cannot display API image from Internet
cell.Food.image = foodImage[indexPath.item] as? UIImage
return cell
}
获取API结果的代码如下:
self.foodImage = json["hits"].arrayValue.map {$0["recipe"]["image"].stringValue}
print(self.foodImage)
【问题讨论】:
-
我认为你从你的 API 中获得了图片的链接。使用 [Kingfisher] github.com/onevcat/Kingfisher 进行图像缓存。
标签: ios swift swift4 swifty-json collectionview