struct ImagePicker: UIViewControllerRepresentable {
typealias UIViewControllerType = UIImagePickerController
var sourceType: UIImagePickerController.SourceType
var completionHandler: (UIImage?) -> Void
func makeUIViewController(context: Context) -> UIImagePickerController {
let imagePicker = UIImagePickerController()
imagePicker.sourceType = sourceType
imagePicker.delegate = context.coordinator
return imagePicker
}
func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {
// No update needed
}
func makeCoordinator() -> Coordinator {
Coordinator(completionHandler: completionHandler)
}
final class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
private let completionHandler: (UIImage?) -> Void
init(completionHandler: @escaping (UIImage?) -> Void) {
self.completionHandler = completionHandler
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
completionHandler(nil)
picker.dismiss(animated: true)
return
}
completionHandler(image)
picker.dismiss(animated: true)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
completionHandler(nil)
picker.dismiss(animated: true)
}
}
}
var body: some View {
TabView {
// Page 1: Map Search
// Page 2: Photos
VStack {
Spacer()
Menu {
Button(action: {
showImagePicker = true
selectedImageSourceType = .photoLibrary
}) {
Label("Choose from Library", systemImage: "photo")
}
Button(action: {
showImagePicker = true
selectedImageSourceType = .camera
}) {
Label("Take Photo", systemImage: "camera")
}
} label: {
Text("Memories")
.font(.title)
.foregroundColor(.black)
}
.padding()
.sheet(isPresented: $showImagePicker, onDismiss: loadImage) {
ImagePicker(sourceType: selectedImageSourceType ?? .photoLibrary) { image in
selectedImage = image
}
}
.padding()
.sheet(isPresented: $showImagePicker, onDismiss: loadImage) {
ImagePicker(sourceType: selectedImageSourceType ?? .photoLibrary) { image in
selectedImage = image
}
}
if !images.isEmpty {
ScrollView {
LazyVGrid(columns: [GridItem(.adaptive(minimum: 200, maximum: 700))], spacing: 20) {
ForEach(images, id: \.self) { image in
Image(uiImage: image)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(height: UIScreen.main.bounds.height / 5)
.frame(width: UIScreen.main.bounds.width - 60)
.cornerRadius(15)
}
}
.padding()
}
} else {
Text("No photos available")
.foregroundColor(.gray)
}
}
.tabItem {
Image(systemName: "camera")
Text("Memories")
}
.tag(1)
}
.accentColor(.blue)
}
@State private var encodedAddress = ""
func fetchLocationInfoFromWikipedia(for address: String) {
guard let encodedAddress = address.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
let url = URL(string: "https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&exintro=true&explaintext=true&titles=\(encodedAddress)") else {
self.encodedAddress = encodedAddress
return
}
URLSession.shared.dataTask(with: url) { data, _, error in
if let data = data {
if let responseJSON = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
let query = responseJSON["query"] as? [String: Any],
let pages = query["pages"] as? [String: Any],
let page = pages.keys.first,
let pageData = pages[page] as? [String: Any],
let extract = pageData["extract"] as? String {
DispatchQueue.main.async {
self.locationInfo = extract
}
}
}
}
.resume()
}
func openWebsite(_ urlString: String) {
if let encodedAddress = self.text.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
let url = URL(string: urlString + encodedAddress) {
UIApplication.shared.open(url)
}
}
func loadImage() {
guard let selectedImage = selectedImage else { return }
images.append(selectedImage)
self.selectedImage = nil
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}