我正在使用 swift 3 并希望在收藏视图中查看照片库图像但这些代码对我不起作用
在看到我的代码之前注意这一点:**我不会收到任何错误或崩溃我只是在收藏视图中看不到我的任何图像库**这是我的代码:
import UIKit
import Photos
class collectionImageViewController: UIViewController , UICollectionViewDataSource , UICollectionViewDelegate {
var imageArray = [UIImage]()
@IBOutlet weak var collectionImageView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewImageCell", for: indexPath) as! CollectionViewImageCell
cell.theImage.image = imageArray[indexPath.row]
return cell
}
func getPhotosFromAlbum() {
let imageManager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
requestOptions.deliveryMode = .highQualityFormat
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let fetchResult: PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)
if fetchResult.count > 0 {
for i in 0..<fetchResult.count {
imageManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 300, height: 300), contentMode: .aspectFill, options: requestOptions, resultHandler: { image, error in
self.imageArray.append(image!)
})
}
} else {
self.collectionImageView?.reloadData()
}
}