Sunday, November 8, 2026
HomeiOS Developmentios - Displaying completely different photos from the identical PhotoPicker

ios – Displaying completely different photos from the identical PhotoPicker

[ad_1]

I am making an attempt to make the person choose pictures (max of three) to show for my relationship app, just like tinder. The issue happens after choosing one picture, which will get displayed on the three add buttons shows as a substitute of just one.

Instance from the Tinder app

What my app exhibits

@State personal var personImage: UIImage?
@State personal var isShowingPhotoPicker = false  

var physique: some View {

   VStack {

       HStack {

           ForEach(0..<3) { personImage in
               Button {
                   isShowingPhotoPicker.toggle()
               } label: {
                   if let personImage = self.personImage 
                       Picture(uiImage: personImage)
                       .resizable()
                   } else {
                       Picture(systemName: "plus.app.fill")
                           .font(.system(dimension: 64))
                   }
                }
             }
          }
       }
    }
    .fullScreenCover(isPresented: $isShowingPhotoPicker, onDismiss: nil) {
            PhotoPicker(personImage: $personImage)
    }

Right here is the PhotoPicker():

struct PhotoPicker: UIViewControllerRepresentable {
    
    @Binding var personImage: UIImage?

    func makeUIViewController(context: Context) -> UIImagePickerController {
        let picker = UIImagePickerController()
        picker.delegate = context.coordinator
        picker.allowsEditing = true
        return picker
    }
    
    func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) { }
    
    func makeCoordinator() -> Coordinator {
        return Coordinator(photoPicker: self)
    }
    
    closing class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
        
        let photoPicker: PhotoPicker
        
        init(photoPicker: PhotoPicker) {
            self.photoPicker = photoPicker
        }
        
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo information: [UIImagePickerController.InfoKey : Any]) {
            if let picture = information[.editedImage] as? UIImage {
                guard let information = picture.jpegData(compressionQuality: 0.2), let compressedImage = UIImage(information: information) else {
                    // return an error present an alert
                    return
                }
                photoPicker.personImage = compressedImage
            } else {
                // return an error present an alert
            }
            picker.dismiss(animated: true)
        }
    }
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments