Skip to content

Instantly share code, notes, and snippets.

@felix-larsen
Last active May 16, 2024 06:58
Show Gist options
  • Save felix-larsen/befb1e1d7d1a8326746309cb8c4a9b11 to your computer and use it in GitHub Desktop.
Save felix-larsen/befb1e1d7d1a8326746309cb8c4a9b11 to your computer and use it in GitHub Desktop.
PHPhotoPicker metadata example
import SwiftUI
import PhotosUI
struct CustomPhotoPickerView: UIViewControllerRepresentable {
@Binding var selectedImage: UIImage?
@Binding var date: Date?
@Binding var location: CLLocationCoordinate2D?
@Environment(\.presentationMode) var presentationMode
func makeUIViewController(context: Context) -> PHPickerViewController {
var config = PHPickerConfiguration(photoLibrary: PHPhotoLibrary.shared())
config.filter = .images
config.selectionLimit = 1
let controller = PHPickerViewController(configuration: config)
controller.delegate = context.coordinator
return controller
}
func makeCoordinator() -> CustomPhotoPickerView.Coordinator {
return Coordinator(self)
}
func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) {
}
class Coordinator: PHPickerViewControllerDelegate {
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
parent.presentationMode.wrappedValue.dismiss()
guard !results.isEmpty else {
return
}
let imageResult = results[0]
if let assetId = imageResult.assetIdentifier {
let assetResults = PHAsset.fetchAssets(withLocalIdentifiers: [assetId], options: nil)
DispatchQueue.main.async {
self.parent.date = assetResults.firstObject?.creationDate
self.parent.location = assetResults.firstObject?.location?.coordinate
}
}
if imageResult.itemProvider.canLoadObject(ofClass: UIImage.self) {
imageResult.itemProvider.loadObject(ofClass: UIImage.self) { (selectedImage, error) in
if let error = error {
print(error.localizedDescription)
} else {
DispatchQueue.main.async {
self.parent.selectedImage = selectedImage as? UIImage
}
}
}
}
}
private let parent: CustomPhotoPickerView
init(_ parent: CustomPhotoPickerView) {
self.parent = parent
}
}
}
struct CustomPhotoPicker_Previews: PreviewProvider {
static var previews: some View {
CustomPhotoPickerView(selectedImage: Binding.constant(nil), date: Binding.constant(nil), location: Binding.constant(nil))
}
}
import SwiftUI
import PhotosUI
struct ContentView: View {
@State var showSheet = false
@State var selectedImage: UIImage?
@State var date: Date?
@State var location: CLLocationCoordinate2D?
var body: some View {
VStack {
Button("Select Image!") {
showSheet.toggle()
}
.padding()
if let image = selectedImage {
Image(uiImage: image)
.resizable()
.frame(width: 200, height: 200)
}
if let date = date {
Text("Creation date: \(date)")
}
if let location = location {
Text("Location: latitude \(location.latitude) longitude \(location.longitude)")
}
}.sheet(isPresented: $showSheet) {
CustomPhotoPickerView(selectedImage: $selectedImage, date: $date, location: $location)
}
}
}
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