Skip to content

Instantly share code, notes, and snippets.

@yimajo
Last active January 24, 2023 06:46
Show Gist options
  • Save yimajo/6485258aa315b21bcd3d2a6eefb6928b to your computer and use it in GitHub Desktop.
Save yimajo/6485258aa315b21bcd3d2a6eefb6928b to your computer and use it in GitHub Desktop.
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() // 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("アセット", asset)
}
}
}
@yimajo
Copy link
Author

yimajo commented Aug 14, 2020

PHAssetの情報を取り出したいならinfo.plistに追記必要

    <key>NSPhotoLibraryUsageDescription</key>
    <string>選択</string>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment