0 Replies
      Latest reply on Dec 15, 2019 4:30 PM by MostaZa
      MostaZa Level 1 Level 1 (0 points)

        Hi! I'm trying to update the value of the progress bar I have created, so I call a completion block of a fuction I have created, the problem is that the completion block is called once the life cicle of the function has expired and I don't get a progress as I should.

        I send you my code.

        Please if anyone has any suggestions I'll appreciate your time.

         

        This is my progress bar

         

        struct ProgressBar: View {
           
            @Binding var value: Float
           
            var body: some View {
                GeometryReader { geometry in
                    ZStack(alignment: .leading) {
                        Rectangle().frame(width: geometry.size.width , height: geometry.size.height)
                            .opacity(0.3)
                            .foregroundColor(Color(UIColor.systemTeal))
                       
                        Rectangle().frame(width: min(CGFloat(self.value)*geometry.size.width, geometry.size.width), height: geometry.size.height)
                        .foregroundColor(Color(UIColor.systemBlue))
                            .animation(.linear)
                       
                    }.cornerRadius(45)
                }
            }
        }

        This is my read image function

        private func readImage(image:UIImage, completionHandler:@escaping(([VNRecognizedText]?,Error?)->Void), comp:@escaping((Double?,Error?)->())) {
            
            var recognizedTexts = [VNRecognizedText]()
            let requestHandler = VNImageRequestHandler(cgImage: (image.cgImage)!, options: [:])
            let textRequest = VNRecognizeTextRequest { (request, error) in
            guard let observations = request.results as? [VNRecognizedTextObservation] else { completionHandler(nil,error)
                return
                }
                for currentObservation in observations {
                    let topCandidate = currentObservation.topCandidates(1)
                    if let recognizedText = topCandidate.first {
                        recognizedTexts.append(recognizedText)
                    }
                }
                completionHandler(recognizedTexts,nil)
            }
            
            textRequest.recognitionLevel = .accurate
            textRequest.recognitionLanguages = ["es"]
            textRequest.usesLanguageCorrection = true
        
            textRequest.progressHandler = {(request, value, error) in
                print(value)
                DispatchQueue.main.async {
                    comp(value,nil)
                }
            }
            try? requestHandler.perform([textRequest])
        }
        

        This is my main view

        struct ContentView: View {
            
            @State var ima = drawPDFfromURL(url: dalai)
            @State private var stepperCounter = 0
            @State private var observations = [VNRecognizedText]()
            @State private var progressValue: Float = 0.0
            private var originaImage = drawPDFfromURL(url: dalai)
            
            var body: some View {
                
                    VStack{
                        VStack { Button(action: {
                            //self.observations = readText(image: self.ima!)
                            DispatchQueue.main.async {
                                readImage(image: self.ima!, completionHandler: { (texts, error) in
                                    self.observations = texts!
                                }) { (value, err) in
                                    self.progressValue = Float(value!)
                                }
                            }
                            })
                            {
                                Text("Read invoice")
                            }
                            ProgressBar(value: $progressValue).frame(height: 20)
                        }.padding()
                        Stepper("Select Next Box", onIncrement: {
                            self.stepperCounter += 1
                            self.ima = addBoundingBoxToImage(recognizedText: self.observations[self.stepperCounter], image: self.originaImage!)
                            print("Adding to age")
                        }, onDecrement: {
                            self.stepperCounter -= 1
                            print("Subtracting from age")
                        })
                        HStack(alignment: .center) {
                            Text("For word: ")
                            Button(action: {getBoxProperties()}) {
                                Text("Get box properties")
                            }
                            
                        }.padding()
                        Image(uiImage: ima!)
                            .resizable()
                            .scaledToFit()
                    }
                
            }
        }
        

         

        I can get the progress printed, but I cannot pass the value.

        Please if anyone has any suggestion I'm all ears.

         

        Thanks

         

        Carlos