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

时间:2018-05-22 03:49:58

标签: ios swift uicollectionview alamofire alamofireimage

美好的一天。

我目前正在使用Alamofire 4.5版和xcode版本9.3。我正在尝试使用此代码将带有自定义标头的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")

在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)")
            }
        }

我遇到的问题是,并非所有图像都会立即加载,尽管我已经使用我的urlrequests数量在alamofire请求中循环。另一件事,当我滚动收集视图时,我从alamofire调用中加载的照片混乱,以便不遵循indexpath.row。此外,我还试图从我的alamofire响应图像中追加图像响应并将其设置为 cell.imageView.image = self.imageArray[indexPath.row]

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

将其加载到集合视图单元格
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
    }

2 个答案:

答案 0 :(得分:1)

美好的一天。我已经找到了解决方案。您需要在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
            }
        }
    }

感谢您的帮助。希望这也有助于遇到同样问题的人。

答案 1 :(得分:0)

self.imageArray[indexPath.row]的错误会因index goes out of bounds而崩溃,因为如果self.booksObject大于图像数组,则单元格中的项目数将等于self.booksObject.count-1

所以你必须确保self.imageArray.count = self.booksObject.count

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
return self.booksObject.count 
}


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
    }