I'm using this GitHub project's controller for displaying user's Photo Gallery inside of UICollectionView. Everything is okay there, but except one: If I run it for the first time, it asks me about permissions. I accept it, but after that I cannot get my images from the gallery. It displays nothing.
But when I close my app and run again it can get my photos from the gallery. So, this problem appears just on the first launch.
Can anyone help me, how can I solve it?
UPDATE
I suppose, that this observer works incorrect:
func photoLibraryDidChange(changeInstance: PHChange) {
let changeDetails = changeInstance.changeDetailsForFetchResult(images)
self.images = changeDetails!.fetchResultAfterChanges
dispatch_async(dispatch_get_main_queue()) {
// Loop through the visible cell indices
let indexPaths = self.imagesCollectionView.indexPathsForVisibleItems()
for indexPath in indexPaths as [NSIndexPath]! {
if changeDetails!.changedIndexes!.containsIndex(indexPath.item) {
let cell = self.imagesCollectionView?.cellForItemAtIndexPath(indexPath) as! ImagesCollectionViewCell
cell.imageAsset = changeDetails!.fetchResultAfterChanges[indexPath.item] as? PHAsset
}
}
}
}
So, I give permissions, but it doesn't show me my photos. For this I need to relaunch my app
If you want any code - I can share it. But I'm using the above github controller which I've attached above.
Since it works correctly when you relaunch the app it sounds like you are probably asking for permissions at the same time you are displaying the view controller/collection view. The problem is that the collection view doesn't know to reload once the user has selected to give your app access to their photos. In the PHPhotoLibrary.requestAuthorization call back you need to tell your view controller/collection view to reload once they have Authorized your app. Small example below:
PHPhotoLibrary.requestAuthorization { status in
switch status {
case .Authorized:
/* RELOAD COLLECTION VIEW HERE */
case .Restricted:
<#your code#>
case .Denied:
<#your code#>
default:
// place for .NotDetermined - in this callback status is already determined so should never get here
break
}
}