i try to show image gallery after user permission to allow all photos, but the gallery is not showing. but when i back to previous controller and navigate back, the gallery show up. but that's not what i want, i want after user allow the image show up.
this my setup
private var allPhotos: [PHAsset] = []
override func viewDidLoad() {
super.viewDidLoad()
PHPhotoLibrary.shared().register(self)
setupCollectionView()
checkPhotoLibraryPermission()
bindViewModel()
}
deinit {
PHPhotoLibrary.shared().unregisterChangeObserver(self)
}
private func bindViewModel() {
let dataSource = Observable.just(allPhotos)
dataSource.asObservable()
.bind(to: collectionView.rx.items(cellIdentifier: GalleryCollectionViewCell.cellId, cellType: GalleryCollectionViewCell.self)) { row, asset, cell in
let imageRequestOptions = PHImageRequestOptions()
imageRequestOptions.resizeMode = .exact
self.imageManager.requestImageDataAndOrientation(for: asset, options: imageRequestOptions) { imageData, _, orientation, info in
guard let imageData = imageData else { return }
cell.setup(imageData: imageData)
}
}.disposed(by: disposedBag)
collectionView.rx.itemSelected
.subscribe(onNext: { [weak self] indexPath in
guard let strongSelf = self else { return }
let asset = strongSelf.allPhotos[indexPath.row]
asset.requestContentEditingInput(with: PHContentEditingInputRequestOptions()) { editingInput, info in
guard let path = editingInput?.fullSizeImageURL?.path.replacingOccurrences(of: "HEIC", with: "PNG") else { return }
self?.imageManager.requestImageDataAndOrientation(for: asset, options: self?.imageRequestOptions) { imageData, _, orientation, info in
guard let imageData = imageData else { return }
self?.goToCropImage(from: imageData, and: path.lastPathComponent)
}
}
}).disposed(by: disposedBag)
}
private func fetchAllPhotos() {
let allPhotosOptions = PHFetchOptions()
allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let fetchResult = PHAsset.fetchAssets(with: .image, options: allPhotosOptions)
allPhotos = fetchResult.objects(at: IndexSet(0..<fetchResult.count))
}
private func checkPhotoLibraryPermission() {
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .authorized:
fetchAllPhotos()
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
case .denied, .restricted :
//handle denied status
gotoAppSettings()
case .notDetermined:
// ask for permissions
PHPhotoLibrary.requestAuthorization { status in
switch status {
case .authorized:
self.fetchAllPhotos()
case .denied, .restricted:
// as above
self.gotoAppSettings()
case .notDetermined:
// won't happen but still
break
case .limited:
break
@unknown default:
fatalError("Failed to get user permission to access photo")
}
}
case .limited:
fetchAllPhotos()
@unknown default:
fatalError("Failed to get user permission to access photo")
}
}
func photoLibraryDidChange(_ changeInstance: PHChange) {
let allPhotosOptions = PHFetchOptions()
let fetchResult = PHAsset.fetchAssets(with: .image, options: allPhotosOptions)
DispatchQueue.main.async {
self.allPhotos = fetchResult.objects(at: IndexSet(0..<fetchResult.count))
self.collectionView.reloadData()
}
}
I already try to to reload collectionView but it still not show up.