UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Error Image Picker

Forums > SwiftUI


import SwiftUI
import UIKit

//view principal
struct ContentView: View {

    @State var imagem: Image? = nil
    @State var personalizacao = ""
    @State var image2: Image? = nil

    var VinhoView: some View {
        VStack {
            Image("pers")
                .resizable()
                .scaledToFit()

        }.overlay(
            Text(personalizacao)
                .font(.title)
                .fontWeight(.semibold)
                .multilineTextAlignment(.center)
                .lineLimit(9)
                .frame(width:200)
                .offset(y:50)
        )
    }

    var body: some View {

        NavigationView {
            ScrollView {

                VStack {

                    HStack {
                        TextField("Digite sua Personalização", text: $personalizacao)
                            .multilineTextAlignment(.center)
                            .font(.title)

                        if personalizacao != ""{
                            Button{
                                personalizacao = ""
                            }
                                label: {Image(systemName: "xmark")
                                    .foregroundColor(.gray)
                                }
                        }
                    } .padding(.horizontal)

                    //

                    ZStack {
                        VStack {
                            Button(action: {
                                self.showCaptureImageView.toggle()
                            }) {
                                Text("Enviar logomarca")
                            }
                            image2?.resizable()
                              .frame(width: 250, height: 250)
                              .clipShape(Circle())
                              .overlay(Circle().stroke(Color.white, lineWidth: 4))
                              .shadow(radius: 10)
                        }
                        if (showCaptureImageView) {
                          CaptureImageView(isShown: $showCaptureImageView, image: $image)
                        }
                    }

                    //

                    VinhoView

                }.padding(.top)

                VStack {
                    if personalizacao != ""{

                        Button{
                            let image = VinhoView.snapshot()

                                           UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
                        }label: {
                            Text("Salvar Imagem")
                                .foregroundColor(.white)
                                .fontWeight(.semibold)
                                .padding()
                                .frame(width:300, height:50)
                                .background(Color.green)
                                .cornerRadius(10)
                        }

                    }
                }
                .padding(.bottom)

            }.navigationBarHidden(true)

        }

    }

}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

extension UIView {
    func makeScreenshot() -> UIImage {
        let renderer = UIGraphicsImageRenderer(bounds: self.bounds)
        return renderer.image { (context) in
            self.layer.render(in: context.cgContext)
        }
    }
}

extension View {
    func snapshot() -> UIImage {
        let controller = UIHostingController(rootView: self)
        let view = controller.view

        let targetSize = controller.view.intrinsicContentSize
        view?.bounds = CGRect(origin: .zero, size: targetSize)
        view?.backgroundColor = .clear

        let renderer = UIGraphicsImageRenderer(size: targetSize)

        return renderer.image { _ in
            view?.drawHierarchy(in: controller.view.bounds, afterScreenUpdates: true)
        }
    }
}

// NOVAS

struct CaptureImageView {

        /// MARK: - Properties
        @Binding var isShown: Bool
        @Binding var image2: Image?

        func makeCoordinator() -> Coordinator {
          return Coordinator(isShown: $isShown, image: $image2)
        }

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

    }

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

    }
}

Coordinator

    import SwiftUI
    class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
      @Binding var isCoordinatorShown: Bool
      @Binding var imageInCoordinator: Image?
      init(isShown: Binding<Bool>, image: Binding<Image?>) {
        _isCoordinatorShown = isShown
        _imageInCoordinator = image
      }
      func imagePickerController(_ picker: UIImagePickerController,
                    didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
         guard let unwrapImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return }
         imageInCoordinator = Image(uiImage: unwrapImage)
         isCoordinatorShown = false
      }
      func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
         isCoordinatorShown = false
      }
    }

Why have an error " Cannot find 'showCaptureImageView' in scope " ?? How can i Fix it ? Thanks.

3      

Because in your ContentView, you never declare a variable called showCaptureImageView.

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.