【问题标题】:CollectionView do not reloading with DKImagePickerControllerCollectionView 不使用 DKImagePickerController 重新加载
【发布时间】:2017-04-07 00:30:21
【问题描述】:

我正在尝试从库中挑选多张图片,并在UICollectionView 上查看。选择图像时它工作正常,但图像没有出现在UICollectionView 上。我尝试再次选择图像,并查看以前的图像。当前选择的不是。

代码如下:

imagePicker.didSelectAssets = {[unowned self] (assets: [DKAsset]) in
    print("Selected!")

    for asset in assets {
        asset.fetchOriginalImageWithCompleteBlock({ (image, info) in
            guard let imageData = UIImageJPEGRepresentation(image!, 0) else {
                print("There is no image bro..!")
                return
            }
            let thumbnails = UIImage(data: imageData)
            self.imageArray.append(thumbnails!)
            print(image!)
        })
    }

    self.collectionView.reloadData()
}

这里是cellForItemAt:

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "selectedImage", for: indexPath) as! SelectedImageCell
    cell.image = imageArray[indexPath.item]
    return cell

}

对于任何想知道我如何解决问题的人。这是代码。

 let group = DispatchGroup()

        for asset in assets {
            group.enter()
            asset.fetchOriginalImageWithCompleteBlock({ (image, info) in

                guard let imageData = UIImageJPEGRepresentation(image!, 0) else {
                    print("There is no image bro..!")
                    return
                }
                let thumbnails = UIImage(data: imageData)
                self.imageArray.append(thumbnails!)
                print(image!)
                group.leave()
            })
        }

        group.notify(queue: .main, execute: {
            print("finish..")
            self.collectionView.reloadData()
        })

【问题讨论】:

  • 您在 for 循环过程完成后重新加载您的集合后尝试。
  • 请出示您的cellForItemAt indexPath 方法。
  • @YogendraGirase 已经尝试过了。
  • 您可以尝试像这样重新加载吗DispatchQueue.main.async {self.collectionView.reloadData()}
  • 并在您的打印语句下方编写重新加载方法print(image!)

标签: ios image uicollectionview


【解决方案1】:

试试这个,

imagePicker.didSelectAssets = {[unowned self] (assets: [DKAsset]) in
        print("Selected!")

        for asset in assets {

            asset.fetchOriginalImageWithCompleteBlock({ (image, info) in

                guard let imageData = UIImageJPEGRepresentation(image!, 0) else {
                    print("There is no image bro..!")
                    return
                }
                let thumbnails = UIImage(data: imageData)
                self.imageArray.append(thumbnails!)
                print(image!)
                self.collectionView.reloadData()
            })
        }
    }

因为asset.fetchOriginalImageWithCompleteBlock 是异步块。

【讨论】:

  • 您的代码是根据循环进程的for循环次数多次重新加载收集和多次收集重新加载是错误的过程。
  • @YogendraGirase ,正确的点 - 但这是知道解决方案是否有效的解决方法,@user3057414 可以根据需要更新 reloadData() 方法以重新加载单个单元格。
  • 啊,好吧,我明白了。我忘记了完整块。谢谢一群人。
猜你喜欢
  • 2017-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
  • 1970-01-01
  • 1970-01-01
  • 2020-06-03
  • 2016-06-05
相关资源
最近更新 更多