Created
October 31, 2020 18:44
-
-
Save rayfix/becca80cb2dc1c5e90496a6b7c61274b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// PhotoPicker2 | |
// | |
// Created by Ray Fix on 10/31/20. | |
// | |
import SwiftUI | |
import PhotosUI | |
import UIKit | |
import Combine | |
class MyViewController: UIViewController { | |
var viewModel: ViewModel = ViewModel() | |
var subscriptions: Set<AnyCancellable> = [] | |
@IBOutlet weak var countLabel: UILabel! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
viewModel.$images | |
.sink { [weak self] images in | |
self?.countLabel.text = String(images.count) | |
} | |
// .sink { [countLabel] images in | |
// DispatchQueue.global(qos: .userInitiated).async { | |
// let text = String(images.count) | |
// DispatchQueue.main.async { | |
// countLabel?.text = text | |
// } | |
// } | |
// } | |
// .receive(on: DispatchQueue.global(qos: .userInitiated)) | |
// .map(\.count) | |
// .map(String.init) | |
// .receive(on: DispatchQueue.main) | |
// .assign(to: \.text, on: countLabel) | |
.store(in: &subscriptions) | |
} | |
@IBAction func showContentViewTapped() { | |
let vc = UIHostingController(rootView: ContentView(viewModel: viewModel)) | |
present(vc, animated: true) | |
} | |
} | |
final class ViewModel: ObservableObject { | |
@Published var images: [UIImage] = [] | |
@Published var showingImagePicker = false | |
} | |
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 images: [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 | |
parent.images = [] | |
for result in results where result.itemProvider.canLoadObject(ofClass: UIImage.self) { | |
result.itemProvider.loadObject(ofClass: UIImage.self) { (image, error) in | |
if let image = image { | |
DispatchQueue.main.async { | |
if let img = image as? UIImage { | |
self.parent.images.append(img) | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
func makeCoordinator() -> MyCoordinator { | |
MyCoordinator(self) | |
} | |
func makeUIViewController(context: Context) -> PHPickerViewController { | |
var configuration = PHPickerConfiguration() | |
configuration.filter = .images | |
configuration.selectionLimit = 10 | |
let picker = PHPickerViewController(configuration: configuration) | |
picker.delegate = context.coordinator | |
return picker | |
} | |
func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) { | |
} | |
} | |
struct ContentView: View { | |
@ObservedObject var viewModel: ViewModel | |
var body: some View { | |
VStack { | |
ScrollView { | |
VStack { | |
ForEach(Array(viewModel.images.enumerated()), id: \.offset ) { offset, image in | |
Image(uiImage: image) | |
.resizable() | |
.scaledToFit() | |
} | |
} | |
} | |
Button("Select Image") { | |
viewModel.showingImagePicker = true | |
}.padding() | |
}.sheet(isPresented: $viewModel.showingImagePicker) { | |
ImagePicker(isShowingPicker: $viewModel.showingImagePicker, images: $viewModel.images) | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView(viewModel: ViewModel()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment