Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save TyrfingMjolnir/5fe6964ef91b72776c12d4479f11017d to your computer and use it in GitHub Desktop.
Save TyrfingMjolnir/5fe6964ef91b72776c12d4479f11017d to your computer and use it in GitHub Desktop.

Note

Info.plist will need the following two lines

<key>NSPhotoLibraryUsageDescription</key>
<string>This app requires access to the photo library.</string>
import UIKit
import PhotosUI
class ViewController: UIViewController {
@IBAction func presentPickerForImagesIncludingLivePhotos(_ sender: Any ) {
presentPicker( filter: PHPickerFilter.images )
}
private func presentPicker( filter: PHPickerFilter ) {
let photoLibrary = PHPhotoLibrary.shared() // Retrieve PHAsset
var configuration = PHPickerConfiguration( photoLibrary: photoLibrary )
configuration.filter = filter
configuration.selectionLimit = 0
let picker = PHPickerViewController( configuration: configuration )
picker.delegate = self
present( picker, animated: true )
}
}
extension ViewController: PHPickerViewControllerDelegate {
func picker(_ picker: PHPickerViewController, didFinishPicking results: [ PHPickerResult ] ) {
dismiss( animated: true )
let identifiers = results.compactMap( \.assetIdentifier )
let fetchResult = PHAsset.fetchAssets( withLocalIdentifiers: identifiers, options: nil )
fetchResult.enumerateObjects { asset, index, pointer in
print( "This asset was selected: \( asset.localIdentifier )" )
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment