Skip to content

Instantly share code, notes, and snippets.

@rayfix
Created October 24, 2020 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rayfix/96deae8e715554560e9e89a8bd48ab71 to your computer and use it in GitHub Desktop.
Save rayfix/96deae8e715554560e9e89a8bd48ab71 to your computer and use it in GitHub Desktop.
import SwiftUI
import PhotosUI
final class ViewModel: ObservableObject {
@Published var image: UIImage?
}
struct Spinner: UIViewRepresentable {
let isAnimating: Bool
func makeUIView(context: Context) -> UIActivityIndicatorView {
let activity = UIActivityIndicatorView(style: .large)
activity.hidesWhenStopped = false
return activity
}
func updateUIView(_ uiView: UIActivityIndicatorView, context: Context) {
isAnimating ? uiView.startAnimating() : uiView.stopAnimating()
}
}
struct ImagePicker: UIViewControllerRepresentable {
@Binding var isShowingPicker: Bool
@Binding var image: UIImage?
final class MyCoordinator: PHPickerViewControllerDelegate {
var parent: ImagePicker
init(_ parent: ImagePicker) {
self.parent = parent
}
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
print("finished")
parent.isShowingPicker = false
for result in results {
dump(result)
if result.itemProvider.canLoadObject(ofClass: UIImage.self) {
result.itemProvider.loadObject(ofClass: UIImage.self) { (image, error) in
if let image = image {
DispatchQueue.main.async {
self.parent.image = image as? UIImage
}
}
}
}
}
}
}
func makeCoordinator() -> MyCoordinator {
MyCoordinator(self)
}
func makeUIViewController(context: Context) -> PHPickerViewController {
var configuration = PHPickerConfiguration()
configuration.filter = .images
configuration.selectionLimit = 1
let picker = PHPickerViewController(configuration: configuration)
picker.delegate = context.coordinator
return picker
}
func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) {
}
}
struct ContentView: View {
@State private var image: UIImage?
@State private var showingImagePicker = false
@State private var spinning = true
var body: some View {
ZStack {
Color.gray
VStack {
if let image = image {
Image(uiImage: image)
.resizable()
.scaledToFit()
}
Button("Select Image") {
showingImagePicker = true
}
Spinner(isAnimating: spinning).onTapGesture {
spinning.toggle()
}
}
}.sheet(isPresented: $showingImagePicker) {
ImagePicker(isShowingPicker: $showingImagePicker, image: $image)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment