【问题标题】:How to display s3 image in collectionView get from API in swift如何在collectionView中显示s3图像从API中快速获取
【发布时间】: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


    【解决方案1】:

    这是一个从 URL 加载图像的好库 SDWebImage

    你可以通过CocoaPodsCocoaPods.org安装它

    安装SDWebImage后,可以通过这个例子加载图片:

    import SDWebImage
    
    imageView.sd_setImage(with: URL(string: "http://www.example.com/path/to/image.jpg"), placeholderImage: UIImage(named: "placeholder.png"))
    

    【讨论】:

      【解决方案2】:

      1 - 在你的ImageCollectionViewCell 中创建一个函数,传递 URL,让单元处理下载部分

      @ImageCollectionViewCell:
      
      func bind(imageUrlPath:String, indexPath:IndexPath) -> Void {
      
      ImageCollectionViewCell.downloadImage(url:imageUrlPath) { (_image) in
              
              DispatchQueue.main.async {
                  
                  if (self.tag == indexPath.row) {
                      
                      self. banner_image.image = _image;
                  }
              }
          }
      }
      

      2 - 使用URLSession从接收到的URL下载图片方法

      @ImageCollectionViewCell:
      
      class func downloadImage(url:String,onImage:@escaping ( _ image:UIImage?)-> Void) -> Void {
                      
              if let imageURL = URL(string: url) {
                  
                  let task = URLSession.shared.dataTask(with: imageURL, completionHandler: { (data, response, error) in
                      
                      if error != nil {
                          return
                      }
                      if let imageData = data {
                          onImage(UIImage(data: imageData));
                      }
                      
                  });
                  task.resume();
              }
          }
      

      3 - 最后在你的控制器中,调用/传递值

      @LandingPageViewController:
      
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
          
          cell.tag = indexPath.row;
      
          cell.bind(imageUrlPath: imageUrl, indexPath: indexPath)
              
      }
      

      【讨论】:

        猜你喜欢
        • 2020-02-06
        • 1970-01-01
        • 2015-12-18
        • 1970-01-01
        • 2019-01-15
        • 1970-01-01
        • 2016-07-18
        • 2023-03-22
        • 1970-01-01
        相关资源
        最近更新 更多