Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/inamiy/swiftui-photopicker

iOS 14 PHPickerViewController wrapper for SwiftUI with data loader support.
https://github.com/inamiy/swiftui-photopicker

Last synced: 29 days ago
JSON representation

iOS 14 PHPickerViewController wrapper for SwiftUI with data loader support.

Awesome Lists containing this project

README

        

# SwiftUI-PhotoPicker
iOS 14 PHPickerViewController wrapper for SwiftUI with data loader support.

## How to Use

```swift
struct ContentView: View
{
@State
private var datas: [PhotoPickerData?] = []

@State
private var isShowingPicker = false

var body: some View
{
VStack(spacing: 40) {
Button("Select Image") {
self.isShowingPicker = true
}

ScrollView {
LazyVGrid(columns: Array(repeating: GridItem(), count: 2)) {
ForEach(datas.enumerated().map { ($0, $1) }, id: \.0) { i, data in
if let image = data?.image {
Image(uiImage: image)
.resizable()
.scaledToFit()
}
else if let videoURL = data?.video {
VideoPlayer(player: AVPlayer(url: videoURL))
.frame(width: 200, height: 200, alignment: .center)
}
else if let livePhoto = data?.livePhoto {
LivePhotoView(livePhoto: .constant(livePhoto))
}
}
}
}
}
.sheet(isPresented: $isShowingPicker) {
PhotoPicker(
datas: $datas,
configuration: pickerConfig,
pattern: pickerPattern
)
}
}
}

private let pickerPattern: PickerPattern = .any(of: [.images, .videos, .livePhotos])

private let pickerConfig: PHPickerConfiguration = {
var config = PHPickerConfiguration()
config.filter = pickerPattern.filter
config.selectionLimit = 0
config.preferredAssetRepresentationMode = .current // required for video
return config
}()
```

## Example

See [Examples](Examples) for more information.

## License

[MIT](LICENSE)