Forked from felix-larsen/CustomPhotoPickerView.swift
Created
January 7, 2021 14:38
-
-
Save byaruhaf/d055cf9e155367568441aef127a6d38e to your computer and use it in GitHub Desktop.
PHPhotoPicker metadata example
This file contains hidden or 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
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)) | |
} | |
} |
This file contains hidden or 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
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