Skip to content

Instantly share code, notes, and snippets.

@FireRayo
Created February 15, 2022 01:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FireRayo/508952f39b5539bba08c8eb5e18ebfbc to your computer and use it in GitHub Desktop.
Save FireRayo/508952f39b5539bba08c8eb5e18ebfbc to your computer and use it in GitHub Desktop.
video player picker - AVPlayer, PHPickerViewController, pick and play images, videos, livephotos - SWIFT, SWIFTUI
// Complete project:
// https://drive.google.com/file/d/1gDRF8xoUmfKXyhDEvWvQx0OXBQuSzFiK/view?usp=sharing
//
// PhotoPicker.swift
// PHPickerViewController_Player_Picker_SwiftUI
//
import SwiftUI
import PhotosUI
// SwiftUI view for PHPickerViewController
struct PhotoPicker: UIViewControllerRepresentable {
typealias UIViewControllerType = PHPickerViewController
@ObservedObject var service: PhotoPickerService
func makeUIViewController(context: Context) -> PHPickerViewController {
var config = PHPickerConfiguration()
config.filter = .any(of: service.filters)
config.selectionLimit = service.selectionLimit
config.preferredAssetRepresentationMode = .current
let controller = PHPickerViewController(configuration: config)
controller.delegate = context.coordinator
return controller
}
func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) {
}
func makeCoordinator() -> Coordinator {
Coordinator(with: self)
}
class Coordinator: PHPickerViewControllerDelegate {
var photoPicker: PhotoPicker
init(with photoPicker: PhotoPicker) {
self.photoPicker = photoPicker
}
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
photoPicker.service.isPresented = false
guard !results.isEmpty else {
return
}
photoPicker.service.results = results
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment