【发布时间】:2021-05-01 16:02:38
【问题描述】:
我是 Swift 的新手。现在,我正在开发一个应用程序,它将从 API 获取数据并在 Swift 中显示。我在 s3 中将图像作为公共存储在后端,并通过使用 API 作为链接传递给 Swift,例如https://example.com/20210501210101.png。我在网上对这个主题做了很多研究,但我找不到合适的方法来显示我的图像。有什么方法可以通过 Swift 中的链接显示从 s3 获取的图像?
API 传递消息
{
"success": true,
"message": "Successfully retrieved image",
"image_detail": [
{
"image": "https://example.com/20210501212429.png",
}
]
}
我正在使用 collectionview 显示我的图像,下面是我的代码示例
extension LandingPageViewController: UICollectionViewDelegate, UICollectionViewDataSource
{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return bannerDetailsItem.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = image_collection_view.dequeueReusableCell(withReuseIdentifier: "cell", for:
indexPath) as! ImageCollectionViewCell
let inputString = BASE_API_URL
let splits = inputString.components(separatedBy: "api")
let imageUrl:NSURL = NSURL(string: splits[0] + "storage/" +
bannerDetailsItem[indexPath.row].Image)!
DispatchQueue.global(qos: .userInitiated).async {
let imageData:NSData = NSData(contentsOf: imageUrl as URL)!
DispatchQueue.main.async {
cell.banner_image.image = UIImage(data: imageData as Data)
}
}
return cell
}
}
谢谢你!
【问题讨论】:
标签: swift api amazon-s3 collectionview