我在两个不同的应用程序中使用UICollectionView。一个UICollectionView被包含,另一个不包含。 我可以使用reloadData()来刷新CollectionView,但是当下面的代码出现在应用程序中时,它坚持使用self.collectionView!.reloadData()和!要么 ?作为“UICollectionView不解开”,然后当视图再次出现时它不重新加载collectionView,只有当我完全相当,并重新启动应用程序。为什么不reloadData()工作?
class SecondViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var imageArray = [UIImage]()
var assetCollection: PHAssetCollection = PHAssetCollection()
let albumName = "Euphoria"
var albumFound : Bool = false
var photosAsset: PHFetchResult<PHAsset>!
override func viewDidLoad() {
super.viewDidLoad()
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)
let collection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
if let _:AnyObject = collection.firstObject{
//found the album
self.albumFound = true
self.assetCollection = collection.firstObject! as PHAssetCollection
}else{
var albumPlaceholder:PHObjectPlaceholder!
//create the folder
NSLog("\nFolder \"%@\" does not exist\nCreating now...", albumName)
PHPhotoLibrary.shared().performChanges({
let request = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: self.albumName)
albumPlaceholder = request.placeholderForCreatedAssetCollection
},
completionHandler: {(success, error)in
if(success){
print("Successfully created folder")
self.albumFound = true
let collection = PHAssetCollection.fetchAssetCollections(withLocalIdentifiers: [albumPlaceholder.localIdentifier], options: nil)
self.assetCollection = collection.firstObject! as PHAssetCollection
}else{
print("Error creating folder")
self.albumFound = false
}
})
}
grabPhotos()
}
override func viewWillAppear(_ animated: Bool) {
self.collectionView!.reloadData()
}
func grabPhotos(){
let imgManager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
requestOptions.deliveryMode = .highQualityFormat
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
if let fetchResult : PHFetchResult = PHAsset.fetchAssets(in: self.assetCollection, options:nil){
if fetchResult.count > 0 {
for i in 0..<fetchResult.count{
imgManager.requestImage(for: fetchResult.object(at: i) as PHAsset, targetSize: CGSize(width:200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {
image, error in
self.imageArray.append(image!)
})
}
}
else{
print("you got no photos!")
}
}
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
let imageView = cell.viewWithTag(1) as! UIImageView
imageView.image = imageArray[indexPath.row]
return cell
}
我已经尝试在ViewWillAppear中添加grabPhotos()方法,但随后它会抓取每次出现视图并复制它们的照片。所以说这张专辑里有10张照片,每次出现的视图都会翻倍。 –
而不是将这些元素附加到imageArray您应该重置imageArray等于新的结果。 –
谢谢托马斯,我是一个完整的初学者,但会试着研究你所说的话并想出一些东西。不完全知道如何去执行你的建议 –