Swift3 集合视图不重新加载(Swift)

Swift3 集合视图不重新加载(Swift),swift3,uicollectionview,reloaddata,Swift3,Uicollectionview,Reloaddata,我正在用swift开发一个应用程序。其中一个选项应该是允许用户选择多张照片并上传,或者拍摄一张照片并创建一篇文章。 我的想法基于以下层次结构: 按下New Photo post按钮->用户将看到UICollection view controller,其中包含其库中的所有照片,以及选择多张照片的选项。我正在重复使用一个单元格,以使用照片库中的图像填充这些集合视图,而照片库上方的图像应该是用户需要单击按钮以允许访问的相机单元格为了拍一张照片,我把相机打开。这是来自模拟器的一个表示 我已经编写了从库

我正在用swift开发一个应用程序。其中一个选项应该是允许用户选择多张照片并上传,或者拍摄一张照片并创建一篇文章。 我的想法基于以下层次结构:

按下New Photo post按钮->用户将看到UICollection view controller,其中包含其库中的所有照片,以及选择多张照片的选项。我正在重复使用一个单元格,以使用照片库中的图像填充这些集合视图,而照片库上方的图像应该是用户需要单击按钮以允许访问的相机单元格为了拍一张照片,我把相机打开。这是来自模拟器的一个表示

我已经编写了从库中获取照片并将其添加到数组中的代码,下面我将展示这些照片的代码。 问题是,当你首次加载应用程序并尝试发布时,会要求你授予对照片的访问权限。不管用户如何选择,不管他们是否同意,他们的集合视图都不会更新

它需要发生什么-当用户被要求授予对照片的访问权限时,他们单击“确定”,它应该在“收集”视图中重新加载数据,但事实并非如此。当他们单击“不允许”时,它将关闭整个视图控制器。这是我的密码

class CVController: UICollectionViewController, UINavigationControllerDelegate, UICollectionViewDelegateFlowLayout {

var photosLibraryArray = [UIImage]()

override func viewDidLoad() {
    super.viewDidLoad()

    grabAllPhotosFromPhoneLibrary()

}

// Grab All Photos from Library
func grabAllPhotosFromPhoneLibrary () {

    let imageManager = PHImageManager.default()

    let requestOptions = PHImageRequestOptions()
    requestOptions.isSynchronous = true  // synchronous works better when grabbing all images
    requestOptions.deliveryMode = .opportunistic

    let fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)] // if false = last image we took would show first

    let fetchResult: PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)

    // 1. Are there photos in the library
    if fetchResult.count > 0 {

        // 2. if fetch.count > 0 it means there's at least 1 photo in the library
        for i in 0..<fetchResult.count {

            // 3. Cycling through the photos
            imageManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler:

                { image, error in
                    // 4. Append each image in the array
                    self.photosLibraryArray.append(image!)
            })
        }

    } else {
        print("You've got no photos")
        self.collectionView?.reloadData()
    }
我试着在viewWillApear、viewDidApear中调用collectionView.reloadData,但没有任何效果

    class CVController: UICollectionViewController, UINavigationControllerDelegate, UICollectionViewDelegateFlowLayout {

    var photosLibraryArray = [UIImage]()

    override func viewDidLoad() {
    super.viewDidLoad()

        checkPhotoLibraryPermission()
    }
添加下面提到的功能 您可以相应地对以下功能进行更改。根据需要。
根据上面发布的代码,只有在没有Photoshot的情况下才调用reloadData使用此self.collectionView?.reloadData,从中可以获得“OK”事件。@Spads我还没有发布实现,因为我个人认为实际上并不需要它。如果您也需要的话,我可以这样做,但它包含了一些基本的实现,包括节数、节中的项目数和项的单元格数。这些图像确实如模拟器中的屏幕截图所示加载,但只有在您授予访问权限后重新运行应用程序时才会发生这种情况Photos@Swifter这有用吗?不,还是不行。此外,这个函数grabAllPhotosFromPhoneLibrary是在viewDidLoad中调用的,因此它是在视图加载时调用的第一个函数。因此,如果用户没有授予任何对照片的访问权限,那么问题就来了。当您第一次加载应用程序时,它会跳转到else语句,并且它不会执行if,除非您关闭应用程序并再次运行它。当用户允许访问照片时,会调用什么?我尝试了viewDidLoad、viewDidAppear,并调用了self.collectionView?。在那里重新加载数据,但无法检查更新的答案。如果有任何冲突,请告诉我。虽然这是未经测试的。我想我已经处理了大部分的支票
    func checkPhotoLibraryPermission() {
        let status = PHPhotoLibrary.authorizationStatus()
        switch status {
        case .authorized: 
        //handle authorized status - // call this function here
                grabAllPhotosFromPhoneLibrary()
            case .denied, .restricted : 
                //handle denied status
            case .notDetermined: 
                // ask for permissions
                PHPhotoLibrary.requestAuthorization() { status in
                    switch status {
                    case .authorized: 
                        // call this function here
                        grabAllPhotosFromPhoneLibrary()

                    case .denied, .restricted: 
                        // as above
                        _ = navigationController?.popViewController(animated: true)
                    case .notDetermined: 
                        // won't happen but still, if you want to handle.
                }
        }
    }
    }
    // Grab All Photos from Library
    func grabAllPhotosFromPhoneLibrary () {

    let imageManager = PHImageManager.default()

    let requestOptions = PHImageRequestOptions()
    requestOptions.isSynchronous = true  // synchronous works better when grabbing all images
    requestOptions.deliveryMode = .opportunistic

    let fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)] // if false = last image we took would show first

    let fetchResult: PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)

    // 1. Are there photos in the library
    if fetchResult.count > 0 {

        // 2. if fetch.count > 0 it means there's at least 1 photo in the library
        for i in 0..<fetchResult.count {

            // 3. Cycling through the photos
            imageManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler:

                { image, error in
                    // 4. Append each image in the array
                    self.photosLibraryArray.append(image!)
            })
        }
    //Use this reload data here as - you want the data loaded in the array first and then show the data.

    self.collectionView?.reloadData()

    } else {
        print("You've got no photos")
        //self.collectionView?.reloadData()

        // if you want to hide the view controller when there is no photo. pop the view controller from your navigation controller like this. 

     _ = navigationController?.popViewController(animated: true)
    }