https://github.com/edonv/swiftuiphpicker
SwiftUI wrapper of PhotoKit's PHPickerViewController.
https://github.com/edonv/swiftuiphpicker
photokit swiftui
Last synced: 8 months ago
JSON representation
SwiftUI wrapper of PhotoKit's PHPickerViewController.
- Host: GitHub
- URL: https://github.com/edonv/swiftuiphpicker
- Owner: edonv
- License: mit
- Created: 2023-06-19T14:48:18.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-22T18:33:57.000Z (almost 2 years ago)
- Last Synced: 2025-04-30T01:08:54.556Z (8 months ago)
- Topics: photokit, swiftui
- Language: Swift
- Homepage:
- Size: 56.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SwiftUIPHPicker
[](https://swiftpackageindex.com/edonv/SwiftUIPHPicker)
[](https://swiftpackageindex.com/edonv/SwiftUIPHPicker)
`SwiftUI` wrapper of [`PhotoKit`](https://developer.apple.com/documentation/photokit)'s [`PHPickerViewController`](https://developer.apple.com/documentation/photokit/phpickerviewcontroller).
The majority of the base of the code came from Hacking With Swift's post on this very topic: .
I added support for more than 1 selection, as well as practical extensions, and hope to extend for ease-of-use with [`PhotosPicker`](https://developer.apple.com/documentation/photokit/photospicker), which is only available in iOS 16+.
## To-Do's
- [x] Add support for multiple selections.
- [ ] Add support for selecting other Live Photos (currently only supports standard photos).
- [x] Add support for videos.
- Currently, this works but requires the developer to use `videoDestination` or `videoDestinationDirectory`. Would it be better to just internally save it to the temporary directory and return that and require that they clean it up after?
- [ ] Rework code to (by default) return `[PHPickerResult]`, then move async mapping code to `async` static function to do separately (or maybe as an extension on `[PHPickerResult]`?)
- Also add interoperability with native newer `PhotosPicker` (and `PhotosPickerItem`/`Transferable`).
## Usage
```swift
import SwiftUI
import SwiftUIPHPicker
struct ContentView: View {
@State private var showPicker = false
var body: some View {
Text("Hello world!")
.sheet(isPresented: $showPicker) {
PHPicker(photoLibrary: .shared()) { result in
switch result {
case .success(let selection):
// Process selected assets
case .failure(let error):
// Deal with error
}
}
.maxSelectionCount(5)
.filter(.all(of: [.images, .not(.livePhotos), .videos]))
.videoDestinationDirectory(DOCUMENT DIRECTORY) /* more code needed for this */
}
}
}
```
### Notes
- If you'd like to support video selections, then you *must* use either the `setVideoDestinationDirectory(_:)` or `setVideoDestinationHandler(_:)` view modifier. Without them, there won't be somewhere set to save the video file and the system's temporary file will be be deleted before it's accessible.