Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionView加载延迟且滚动非常不平稳_Ios_Swift_Scroll_Uicollectionview - Fatal编程技术网

Ios UICollectionView加载延迟且滚动非常不平稳

Ios UICollectionView加载延迟且滚动非常不平稳,ios,swift,scroll,uicollectionview,Ios,Swift,Scroll,Uicollectionview,我正在设计一个集合视图,在该视图中,我试图加载存储在数组中的字典中的图像和文本。我这里的主数组,即banneraray正在其他函数中解析。但我遇到的问题是,当我滚动它来加载图像时,我的集合视图变得起伏不平,好像在滚动的道路上有颠簸。我已经寻找了各种答案和代码,但没有一个能解决我的问题 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) ->

我正在设计一个集合视图,在该视图中,我试图加载存储在数组中的字典中的图像和文本。我这里的主数组,即
banneraray
正在其他函数中解析。但我遇到的问题是,当我滚动它来加载图像时,我的集合视图变得起伏不平,好像在滚动的道路上有颠簸。我已经寻找了各种答案和代码,但没有一个能解决我的问题

 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
    }

请帮忙!提前感谢。

问题在于您使用了NSData(contentsOfURL:)方法,该方法同步加载图像并阻止UI。像AFNetworking提供的异步方法不会导致此故障

NSData(contentsOfURL:url) 
在主线程上下载图像,这将阻止主线程更新UI。您应该使用
NSOperation
子类下载图像。雷·温德里希对这个问题有自己的看法。您还可以使用其他替代方案,例如

希望有帮助

编辑:

正如Sunil在上面指出的,您不需要在
dispatch\u async
块中完成大部分工作。您需要异步执行的唯一任务是图像下载。当您准备好更新
UIImageView

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(contentsofull:url)
在主线程中下载图像这就是为什么
UICollectionView
加载延迟或可能被阻止的原因只需做一件事创建下载图像的队列并在主线程中设置它。

lndngimg
文本视图还是
textField
如果是,将其更改为
标签
数据=NSData(contentsOfURL:url)
在主线程中下载图像,这就是为什么
UICollectionView
加载延迟或可能被阻止的原因。只需做一件事创建下载图像的队列并将其设置在主线程中。此外,您不需要将这些代码放在dispatch_main()中,它已经在主队列中,并且层操作要长很多次。如果可能的话,把它们放在那个单元格的awakeFromNib中。@SunilChauhan请解释一下,因为我正在执行异步类型。那么问题出在哪里?@KamalaDash:Bunty-Madan的评论是正确的:
NSData(contentsofull:url)
正在主线程上从服务器下载数据,阻塞主线程会导致用户界面卡住。因此,删除该行并异步下载图像(即在后台线程中)。对于图像下载,我推荐使用library,它可以异步管理图像下载和缓存。!请看一看在AlamoFire中是否有可能,因为我正在尝试做这方面的所有工作。那么卷轴呢?是的,它可以工作,请看问题是您正在主线程中下载,因为您的卷轴很重,所以只需在saparte队列中下载内容,然后在主队列中更新UI。