【问题标题】:Grabbing images from API从 API 抓取图像
【发布时间】:2017-03-06 10:08:23
【问题描述】:

我想从提供图像 url 的 api 中获取图像,我需要另一个 api 调用来从他们的 url 下载图像吗?我如何抓取每个图像并将其附加到数组中并将它们显示在我的集合视图单元格中。我已经在类范围之外声明了这一点 var videoArray = [funnyVideos]()

Alamofire.request(_url).responseJSON { (response) in

    print(response.result.value!)
    if let dict = response.result.value as? Dictionary<String, AnyObject> {
        if let result = dict["result"] as? Dictionary<String, AnyObject> {
            if let data = result["data"] as? [Dictionary<String, AnyObject>] {

                for obj in data {
                    let mainObj = funnyVideos()
                    mainObj.parseData(from: obj)
                    videoArray.append(mainObj)
                    // print (obj)
                }

                if let image1 = data[0]["image"] as? String {        
                    print(image1)
                }

                self.collection.reloadData()   
            }   
        }
    }
    completed()
}

这个函数是另一个类:搞笑视频

func parseData(from: Dictionary<String, AnyObject>){
    if let name = from["title"] as? String{
        self._name = name.capitalized  
    }
}

这个函数配置单元是另一个类,它包含集合单元的标签和图像视图的出口。

func configureCell(_ funny : funnyVideos) {
    self.funny = funny
    nameLabel.text = self.funny.name.capitalized
    //   imageView.image = UIImage()
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt      indexPath: IndexPath) -> UICollectionViewCell {
    fun = videoArray[indexPath.row]
    cell.configureCell(fun)
}

我有什么选项可以从 UIImage 容器中的 url 获取它们? print(image1) 打印图像的 url。

【问题讨论】:

  • 修复代码格式

标签: ios swift3 uiimage xcode8 uicollectionviewcell


【解决方案1】:

试试看,

您可以使用 AlamofireImage 下载图像并设置为 collectionview。

【讨论】:

    【解决方案2】:

    是的,您必须对图像的 url 进行另一个 api 调用。您可以使用 AlamofireImage 或 sdwebimage 库来下载图片。

    首先使用普通的 Alamofire 库点击 Api 并获取所有图像的链接并将它们保存在 dict 或 array 中。然后使用 AlamofireImage 或 sdwebimage 进行另一个 api 调用。

    你应该试试 sdwebimage 因为它很简单

    【讨论】:

      【解决方案3】:

      您应该使用名为 Kingfisher 的 Swift 库 当您提供 URL 时,它将处理图像的缓存和下载。 这是入门的链接。 Cheat Sheet

      【讨论】:

        【解决方案4】:

        是的,您需要调用从 json 获取的图像的 url,要从 url 获取图像,您可以使用 像这样使用

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

            guard let identifier = cellIdentifier else{
                fatalError("Cell identifier not provided")
            }
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier ,
                                                                             for: indexPath) as UICollectionViewCell
        

        让 imageUrl = arrayImage[indexPath.row]

        cell.imageView.setImage(url:"imageUrl")

        返回单元格

        }

        https://github.com/rs/SDWebImage https://github.com/onevcat/Kingfisher

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-15
          • 2015-02-17
          相关资源
          最近更新 更多