1

我有一个应该显示相机胶卷照片的集合视图,但它没有显示任何东西!我看到了类似的问题,但是他们的代码不起作用!我在 Info.plist 中添加了使用相册照片的隐私这是我的代码

class ViewController: UICollectionViewController , UICollectionViewDelegateFlowLayout {



override func viewDidLoad() {
    grabPhotos()
}
func grabPhotos() {

    var imageArray = [UIImage]()

    let imgManager = PHImageManager.default()

    let requestOptions = PHImageRequestOptions()

    requestOptions.isSynchronous = true

    requestOptions.deliveryMode = .highQualityFormat

    let fetchOptions = PHFetchOptions()

    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]

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



        if fetchResult.count > 0 {


            for i in 0..<fetchResult.count {

                imgManager.requestImage(for: fetchResult.object(at: i) , targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {


                    image , error in



                    imageArray.append(image!)

                })




            }







        }
        else {

            print("No Photos")
            self.collectionView?.reloadData()

        }



    }


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArray.count


    }

        func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath)


            let imageView = cell.viewWithTag(1) as! UIImageView


             imageView.image = imageArray[indexPath.row]


             return cell


    }

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout:
    UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {

    let width = collectionView.frame.width / 3 - 1




    return CGSize(width: width, height: width)


}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 1.0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 1.0 
}
}

它什么也没显示我读过类似的问题但他们没有帮助我

4

0 回答 0