Merge pull request #499 from RyanRory/fix-crash-on-photo-selection

Fix the crash on photo selection
pull/501/head
RyanZhao 3 years ago committed by GitHub
commit 1c70ceac01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -221,6 +221,10 @@ class PhotoCollectionContents {
class PhotoCollection {
private let collection: PHAssetCollection
// The user never sees this collection, but we use it for a null object pattern
// when the user has denied photos access.
static let empty = PhotoCollection(collection: PHAssetCollection())
init(collection: PHAssetCollection) {
self.collection = collection
@ -275,11 +279,30 @@ class PhotoLibrary: NSObject, PHPhotoLibraryChangeObserver {
deinit {
PHPhotoLibrary.shared().unregisterChangeObserver(self)
}
private lazy var fetchOptions: PHFetchOptions = {
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "endDate", ascending: true)]
return fetchOptions
}()
func defaultPhotoCollection() -> PhotoCollection {
guard let photoCollection = allPhotoCollections().first else {
owsFail("Could not locate Camera Roll.")
var fetchedCollection: PhotoCollection?
PHAssetCollection.fetchAssetCollections(
with: .smartAlbum,
subtype: .smartAlbumUserLibrary,
options: fetchOptions
).enumerateObjects { collection, _, stop in
fetchedCollection = PhotoCollection(collection: collection)
stop.pointee = true
}
guard let photoCollection = fetchedCollection else {
Logger.info("Using empty photo collection.")
assert(PHPhotoLibrary.authorizationStatus() == .denied)
return PhotoCollection.empty
}
return photoCollection
}

Loading…
Cancel
Save