在Button Pressed上重新加载CollectionView数据

时间:2018-03-22 20:02:55

标签: ios swift uicollectionview

我有一个相机应用程序,可以在实时相机Feed UIView顶部的 UICollectionView 中显示用户照片库的缩略图。

我希望我可以轻松地刷新'每次用户使用reloadData()属性拍摄新照片时,UICollectionView的内容:

@IBAction func snapNewPhoto(_ sender: Any) {
    takePhotoMethod()
    myCollectionView.reloadData()
    myCollectionView.collectionViewLayout.invalidateLayout()
}

这是我的takePhoto方法:

func takePhotoMethod () {
    if let videoConnection = sessionOutput.connection(with: AVMediaType.video) {
        sessionOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: {
            buffer, error in

            let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer!)
            self.imageArray.append(UIImage(data: imageData!)!)

            print ("Photo taken", self.imageArray.count)
        })
    }
}

这不起作用,我想解决方案有点复杂,我是否需要重新加载'整个 View Controller 重新加载 UICollectionView 还是还有另一种更有说服力的方法?

1 个答案:

答案 0 :(得分:0)

func takePhotoMethod () {
if let videoConnection = sessionOutput.connection(with: AVMediaType.video) {
    sessionOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: {
        buffer, error in

        let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer!)
        self.imageArray.append(UIImage(data: imageData!)!)
        myCollectionView.reloadData()//reload must be done in here.
        print ("Photo taken", self.imageArray.count)
    })
}

}