Ios Swift-AlamofireImage下载多个URL并显示在CollectionView中
我想从数组中的字符串下载多个URL,并在Ios Swift-AlamofireImage下载多个URL并显示在CollectionView中,ios,swift,uicollectionview,alamofire,alamofireimage,Ios,Swift,Uicollectionview,Alamofire,Alamofireimage,我想从数组中的字符串下载多个URL,并在collectionView中显示图像。我成功地显示了四幅图像(屏幕上有四个可见单元格),当我滚动时,开始下载另外两幅图像 我希望这6个图像在同一时间下载(我不必滚动开始其他下载)。在此之后,我想在我的收藏视图中显示下载和显示图像所花费的总时间 我做错了什么 这是我的密码: import UIKit import AlamofireImage import Alamofire class ViewController: UIViewController,
collectionView
中显示图像。我成功地显示了四幅图像(屏幕上有四个可见单元格),当我滚动时,开始下载另外两幅图像
我希望这6个图像在同一时间下载(我不必滚动开始其他下载)。在此之后,我想在我的收藏视图中显示下载和显示图像所花费的总时间
我做错了什么
这是我的密码:
import UIKit
import AlamofireImage
import Alamofire
class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
var players = [
["image_Name":"https://images.unsplash.com/photo-1420768255295-e871cbf6eb81?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=4b94ef340bd7f6cac580fbc76af326af"],
["image_Name":"https://images.unsplash.com/photo-1465411801898-f1a78de00afc?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=149d27223217c0fa63c7dd8f1e8d23f6"],
["image_Name":"https://images.unsplash.com/photo-1466853817435-05b43fe45b39?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=a3b629b7e0c4f710ce119f219ae5b874"],
["image_Name":"https://images.unsplash.com/photo-1467404899198-ccadbcd96b91?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=66eef8db7a0aa4119c6c8d7ba211f79f"],
["image_Name":"https://images.unsplash.com/photo-1470322346096-ecab3914cab7?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=83863ba23662871baf6434c6000e00bd"],
["image_Name":"https://images.unsplash.com/photo-1473624566182-509e437512f4?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=54c3ec6d1fee824d62e6fa76676ddf17"]
]
var methodStart = Date()
@IBOutlet weak var mycall: UICollectionView!
var selectedIndex=[Int]()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.players.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell:playerCell = collectionView.dequeueReusableCell(withReuseIdentifier: "playerCell", for: indexPath) as! playerCell
let url = URL(string:self.players[indexPath.row]["image_Name"]!)
if indexPath.row == 0 {
methodStart = Date()
}
Alamofire.request(url!).responseImage { response in
// debugPrint(response)
// print(response.request)
// print(response.response)
// debugPrint(response.result)
if let image = response.result.value {
cell.playerImage.image = image
}
}
if indexPath.row == self.players.count - 1 {
let methodFinish = NSDate()
let executionTime = methodFinish.timeIntervalSince(methodStart as Date)
print("Execution time: \(executionTime)")
}
return cell
}
}
代码中的问题-
您说过您希望同时下载这6幅图像,当用户滚动时,第5幅和第6幅图像应立即显示。但这并没有发生,因为当您滚动时,第5个和第6个单元格的cellForItemAtIndexPath
函数被调用。这是默认行为,在用户滚动之前不能调用它
快速解决方案-
下载cellForItemAtIndexPath
功能之外的所有图像,如viewDidLoad
或ViewDidAspect
中的图像,方法是循环播放数组,然后在下载完最后一个后重新加载收藏视图
永久性最佳解决方案-
使用或在单元格中显示图像,实现图像的延迟加载和缓存。并在这些库中使用prefetch
选项在图像显示在单元格中之前提取图像。要使用预取选项,您需要在数组中循环,并将URL传递给库的获取和存储功能,然后根据库的语法将图像加载到单元格中,它将在图像下载时管理图像的显示,您不必担心这一点。当您处理网络调用时,您应该更新主队列中的UI。我想在这里添加的另一个有效点是Alamofire有一个用于图像下载/调整大小的特殊库。请看一看
下载后,更新主队列中的映像,并为映像设置NeedsLayout属性
Alamofire.request(url!).responseImage { response in
if let image = response.result.value {
DispatchQueue.main.async {
cell.playerImage.image = image
cell.playerImage.setNeedsLayout()
}
}
}