TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

How to access EXIF data?

Forums > SwiftUI

Hi all, So i have this code that allows me to select an image from the photo library and then display it. But my question is, how can i access the EXIF data? More specific the Latitude and Longitude ?


struct imagePicker:UIViewControllerRepresentable {
    @Binding var image: UIImage?
    @Binding var showImagePicker: Bool

    typealias UIViewControllerType = UIImagePickerController
    typealias Coordinator = imagePickerCoordinator

    var sourceType:UIImagePickerController.SourceType = .camera

    func makeUIViewController(context: UIViewControllerRepresentableContext<imagePicker>) -> UIImagePickerController {
        let picker = UIImagePickerController()
        picker.sourceType = sourceType
        picker.delegate = context.coordinator
        return picker
    }

    func makeCoordinator() -> imagePicker.Coordinator {
        return imagePickerCoordinator(image: $image, showImagePicker: $showImagePicker)
    }

    func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<imagePicker>) {}

}

class imagePickerCoordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
        @Binding var image: UIImage?
        @Binding var showImagePicker: Bool

    init(image:Binding<UIImage?>, showImagePicker: Binding<Bool>) {
            _image = image
            _showImagePicker = showImagePicker
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let uiimage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            image = uiimage
            showImagePicker = false
        }

    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        showImagePicker = false
    }

}
struct ContentView: View {
    @State var showActionSheet = false
    @State var showImagePicker = false

    @State var sourceType:UIImagePickerController.SourceType = .camera

    @State var image:UIImage?

    var body: some View {

        VStack {
            if image != nil {
                Image(uiImage: image!)
                .resizable()
                .scaledToFit()
                    .frame(width:150, height:150)
            } else {
                Image(systemName: "person")
                .resizable()
                .scaledToFit()
                    .frame(width:150, height:150)
            }
            Button(action: {
                self.showActionSheet = true
            }) {
                Text("Show Image Picker")
            }.actionSheet(isPresented: $showActionSheet){
                ActionSheet(title: Text("Add a picture to your post"), message: nil, buttons: [
                    //Button1

                    .default(Text("Camera"), action: {
                        self.showImagePicker = true
                        self.sourceType = .camera
                    }),
                    //Button2
                    .default(Text("Photo Library"), action: {
                        self.showImagePicker = true
                        self.sourceType = .photoLibrary
                    }),

                    //Button3
                    .cancel()

                ])
            }.sheet(isPresented: $showImagePicker){
                imagePicker(image: self.$image, showImagePicker: self.$showImagePicker, sourceType: self.sourceType)

            }
        }

    }
}

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.