UICollectionView加载速度如此之慢

时间:2018-08-12 00:57:36

标签: ios swift swift3 uicollectionview segue

我已经成功地从数组JSON返回的对象中调用图像的实例,UICollection的加载速度非常慢,尤其是当它包含主图像时。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


    let childDict: NSDictionary = subCategoryData .object(at: indexPath.row) as! NSDictionary

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listcollectionview", for: indexPath) as! subCategoryCollectionViewCell

    subCategoryTable.register(UINib(nibName: "subCategoryCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "listcollectionview")

    let subimages = childDict.object(forKey: "image") as! String!
    let data = NSData(contentsOf: NSURL(string: subimages!)! as URL)
    cell.backgroundImageView.image = UIImage(data: data! as Data)

    cell.categoryName.text = (subCategoryMenuData [indexPath.row] as? String)
    cell.categoryName?.textColor = UIColor.white
    cell.categoryName?.backgroundColor = UIColor().HexToColor(hexString: GREYBLACK)

    return cell;
}

我在调用segue时也尝试了didselectitemat中的dispatch.queue,但这并不能解决问题

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let childDict: NSDictionary = subCategoryData .object(at: indexPath.row) as! NSDictionary

    if (childDict.object(forKey: "children") as! NSArray).count > 0{
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let initViewController: subCategory? = (sb.instantiateViewController(withIdentifier: "subCategory") as? subCategory)
        initViewController?.subCategoryData = (childDict.object(forKey: "children") as! NSArray)
        initViewController?.subName = childDict.object(forKey: "name") as! String!
        initViewController?.subId = childDict.object(forKey: "path") as! String!
        initViewController?.modalTransitionStyle = .flipHorizontal
        self.navigationController?.pushViewController(initViewController!, animated: true)
    }else{

        categoryName = childDict .object(forKey: "name") as! String
        categoryId = childDict .object(forKey: "path") as! String
        DispatchQueue.main.async {
            self.performSegue(withIdentifier: "productCategorySegue",sender: self)
        }
    }


}

加载有时需要30秒

4 个答案:

答案 0 :(得分:2)

您可以在单元格中加载图像以进行索引,您可以使用sdwebimage库安装theough pods进行延迟加载。肯定会解决您的问题。

答案 1 :(得分:1)

哦,我刚刚找到了解决方案,这要感谢@abhishekkharwar帖子here

我将ObjC转换为Swift 3以解决该问题。

DispatchQueue.global(qos: .default).async(execute: {() -> Void in
    var image = UIImage(contentsOfFile: frontPath)
    DispatchQueue.main.async(execute: {() -> Void in
        frontButton.setBackgroundImage(image, for: .normal)
    })
})

答案 2 :(得分:0)

使用AlamofireImage库:

let subimages = childDict.object(forKey: "image") as! String!
if let imageUrl = URL(string: subimages){
     cell.backgroundImageView.af_setImage(withURL: imageUrl, placeholderImage: nil)
}

答案 3 :(得分:0)

-----快速4 -----

第1步:添加此扩展程序::如果您不知道扩展程序,请检查以下内容:Swift extension example

 extension UIImageView {
        func downloadImage(from url:String){

            if url != ""{
            let urlRequest = URLRequest(url: URL(string:url)!)

            let task = URLSession.shared.dataTask(with: urlRequest){(data,response,error) in

                if error != nil{
                    return
                }
                DispatchQueue.main.async {
                    self.image = UIImage(data: data!)
                }
            }
            task.resume()
            }
        }
    }

第2步:使用UIImageViewExtension下载图像: 在您的“ cellForItemAt indexPath”方法中,使用以下代码:

cell.backgroundImageView.downloadImage(from: data)

其他:检查图像文件的大小。如果尺寸大的话,加载时间会很长。您可以在图像加载之前添加此平滑的图像外观动画,以使其酷炫。

UIView.transition(with: cell.backgroundImageView,
                              duration: 0.5,
                              options: .transitionCrossDissolve,
                              animations: { cell.backgroundImageView.downloadImage(from: data) },
                              completion: nil)