gpt4 book ai didi

ios - UICollectionView 加载较晚并且滚动非常颠簸

转载 作者:行者123 更新时间:2023-11-30 13:48:02 28 4
gpt4 key购买 nike

我正在设计一个 Collection View ,在其中尝试加载存储在数组中的字典中的图像和文本。我的主数组在这里,即 bannerarray 正在其他函数中进行解析。但我遇到的问题是,当我滚动它来加载图像时,我的 Collection View 变得不稳定,就好像滚动道路上有颠簸一样。我寻找了各种答案和代码,但没有一个能解决我的问题。

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = myCollectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! LandingPageCVC


dispatch_async(dispatch_get_main_queue(),
{
let categoryname = self.categoriesarray.objectAtIndex(indexPath.row).valueForKey("name")

cell.layer.shouldRasterize = true;
cell.layer.rasterizationScale = UIScreen.mainScreen().scale



let imageurl = self.categoriesarray.objectAtIndex(indexPath.row).valueForKey("image") as! String

if let url = NSURL(string: imageurl),
data = NSData(contentsOfURL: url)
{

cell.lndngimage.image = UIImage(data: data)
}
if indexPath.row % 2 == 0
{
cell.backgroundColor = UIColor(red: 67/255.0, green: 179/255.0, blue: 246/255.0, alpha: 1.0)
}
else if indexPath.row % 3 == 0 && indexPath.row%2 != 0
{
cell.backgroundColor = UIColor.redColor()
}
else
{
cell.backgroundColor = UIColor.greenColor()
}
cell.lndngimg.text = categoryname as? String

})



return cell
}

请帮忙!提前致谢。

最佳答案

let queue = dispatch_queue_create("loader", nil)
dispatch_async(queue) { () -> Void in
//download the image do stuff
//like data = NSData(contentsOfURL: url)

//move back in main thread
dispatch_async(dispatch_get_main_queue(),{
//set the image.
//cell.lndngimage.image = UIImage(data: data)
})
}

因此 data = NSData(contentsOfURL: url) 在主线程中下载图像,这就是为什么 UICollectionView 加载较晚或可能会阻塞,只需做一件事创建下载图像的队列并将其设置在主线程中。

关于ios - UICollectionView 加载较晚并且滚动非常颠簸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34677709/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com