ios - 使用 Alamofire responseImage IOS swift 从 URLRequest 加载图像

标签 ios swift uicollectionview alamofire alamofireimage

美好的一天。

我目前使用的是 Alamofire 4.5 版和 xcode 9.3 版。我正在尝试使用此代码将带有自定义 header 的 URLrequest 中的图像加载到带有来自文件服务器的图像的 Collection View

var imageRequest = URLRequest(url: URL(string: "myurl here ")!)
imageRequest.addValue(MyVariables.accountToken, forHTTPHeaderField: "accountToken")

imageRequest.addValue("header value 1", forHTTPHeaderField: "header field 1")

imageRequest.addValue("header value 2", forHTTPHeaderField: "header field 2")

imageRequest.addValue(imageurl from fileserver, forHTTPHeaderField: "file")

将 header 添加到 urlrequest 后,我​​使用这个 alamofire responseimage 为图像设置值

            Alamofire.request(imageURLRequest).responseImage { response in
            if let image = response.result.value {
                self.count += 1
                print("image  \(self.count )", image)
            } else {
                print("unable to load   \(self.count)")
            }
        }

我遇到的问题是,尽管我已经在 alamofire 请求中循环了我拥有的 urlrequests,但并非所有图像都在一次加载。另一件事,当我滚动 collectionview 时,我从 alamofire 调用加载的照片困惑,以便不遵循 indexpath.row。此外,我还尝试附加来 self 的 alamofire 响应图像的图像响应并将其设置为 cell.imageView.image = self.imageArray[indexPath.row]

但它超出了界限。当我记录 alamofire imageresponse 时,它​​只加载到索引 7。我尝试了不同的方法,例如将 urlrequest 附加到数组并通过

将其加载到 Collection View 单元格
cell.imageView.af_setImage(withURLRequest: imageURLRquest)

但它只加载图像数组中的第一项。很抱歉这个问题很长,因为我已经尝试了迄今为止找到的大部分解决方案,但它并没有解决我现在遇到的问题。请提供解决此问题的建议。提前谢谢你。

更新:

这是来自 alamofire 请求的数据源的代码。它返回 10 个附加到数组的 urlrequest

var imageRequest = URLRequest(url: URL(string: "myurl here ")!)
imageRequest.addValue(MyVariables.accountToken, forHTTPHeaderField: "accountToken")

imageRequest.addValue("header value 1", forHTTPHeaderField: "header field 1")

imageRequest.addValue("header value 2", forHTTPHeaderField: "header field 2")

imageRequest.addValue(imageurl from fileserver, forHTTPHeaderField: "file")

self.imageArray.append(imageRequest)

这是用于 cellForItemAt

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BooksCell", for: indexPath) as! LibraryCollectionViewCell

        cell.imageView.af_setImage(withURLRequest: self.imageArray[indexPath.row])
        cell.bookName.text = self.booksObject[indexPath.row].object(forKey: "title") as! String
        cell.cosmosRatingView.rating = self.booksObject[indexPath.row].object(forKey: "rating") as! Double

        return cell
    }

最佳答案

美好的一天。我已经找到了解决方案。您需要在 cellForItemAt 处将 imageView 的值重置为 nil,然后将其分配给 indexPath.row

 if let image = self.bookImages[indexPath.row]  {
        cell.imageView.image = image
    } else {
        Alamofire.request(imageURLRequest).responseImage { response in
            if let image = response.result.value {
                cell.imageView.image = image
                self.bookImages[indexPath.row]  = image
            } else {
                cell.imageView.image = placeholderImage
            }
        }
    }

谢谢大家的帮助。希望这也能帮助遇到同样问题的人。

关于ios - 使用 Alamofire responseImage IOS swift 从 URLRequest 加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50459490/

相关文章:

iphone - 如何下载和设置不在主线程上的 UIImage

ios - 保存核心数据条目,它们不能直接用于获取,为什么?

ios - 如何从 UICollectionViewCell 中的按钮关闭 UICollectionViewController

ios - 在闭包中快速捕捉 self

objective-c - UICollectionView 部分标题崩溃 iOS 8

ios - CollectionView 单元格在选中时移动

ios - 在 Objective C 中创建全局变量

ios - 在函数外更改 var 的值

ios - 添加项目(核心数据)时更新 UICollectionView,卡在 insertItemsAtIndexPaths 上

ios - 将所选图像放入网格