Ios UICollectionView加载速度太慢了
我成功地从数组JSON调用了镜像的实例返回的对象UICollection加载速度非常慢,特别是当它有主镜像时Ios UICollectionView加载速度太慢了,ios,swift,swift3,uicollectionview,segue,Ios,Swift,Swift3,Uicollectionview,Segue,我成功地从数组JSON调用了镜像的实例返回的对象UICollection加载速度非常慢,特别是当它有主镜像时 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let childDict: NSDictionary = subCategoryData .object(at: indexPath.
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秒哦,多亏了@abhishekkharwarpost,我才找到了解决方案 我将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)
})
})
哦,多亏了@abhishekkharwar post,我才找到了解决方案 我将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)
})
})
在单元格中加载图像作为索引,可以使用sdwebimage库安装用于延迟加载的ough pods。它肯定会解决您的问题。您在单元格中加载图像作为索引,您可以使用sdwebimage库安装用于延迟加载的ough pods。它肯定会解决您的问题。使用
AlamofireImage
库:
let subimages = childDict.object(forKey: "image") as! String!
if let imageUrl = URL(string: subimages){
cell.backgroundImageView.af_setImage(withURL: imageUrl, placeholderImage: nil)
}
使用阿拉莫菲图像库:
let subimages = childDict.object(forKey: "image") as! String!
if let imageUrl = URL(string: subimages){
cell.backgroundImageView.af_setImage(withURL: imageUrl, placeholderImage: nil)
}
----Swift 4-----
步骤1:添加此扩展名:如果您不知道扩展名,请检查以下内容:
步骤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)
----Swift 4-----
步骤1:添加此扩展名:如果您不知道扩展名,请检查以下内容:
步骤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)
或者AlamofireImageI在我的LIB中安装了AlamorfireImage。非常感谢您的示例。在此或AlamofireImageI之后,我的LIB中安装了AlamorfireImage。非常感谢您的示例。下面是
let data=NSData(contentsOf:NSURL(string:subimages!)!as URL)
,您能解释一下为什么在主线程上同步执行此操作吗?如果每次为集合视图创建单元格时,都要了解注册nib背后的想法也会很好…?在加载所有图像和页面时使用它加载占位符。那么代码在哪里加载图像/页面?您是否也打算与我们分享这一点?let data=NSData(contentsOf:NSURL(string:subimages!)!as URL)
,您能解释一下为什么在主线程上同步执行此操作吗?另外,在每次为集合视图创建单元格时,理解注册nib背后的想法也很好…?在加载所有图像和页面时使用它加载占位符。代码在哪里?然后加载图像/页面的是什么?你也打算和我们分享一下吗?