2017-10-05 105 views
0

当另一个结束时,我试图调用一个函数,但我不断收到一个错误,告诉我图像不高于0像素高和宽,所以我认为它没有得到调用函数之前的图像。当我添加一个断点来调用OCR的功能时,此时应用程序不会在屏幕上显示图像,这就是为什么我得出这个结论的原因。当另一个结束时运行一个函数,iOS,Swift

这里是我从ocr得到的错误。

NSAssert(widthOfImage> 0 & & heightOfImage> 0,@ “合格图像不能为空 - 它应该是至少1px的高和宽”);

下面是我的控制台读数,我在其中放置打印件以查看流量​​。

托尼1请求.... 托尼3运行OCR .... 托尼2手柄矩形.... 托尼:这里Corected图像...... (LLDB)

以下是我的代码。我应该有一个完成,确保函数不被调用,直到图像到位?

func startOCR() { 
     swiftOCRInstance.recognize(correctedImageView.image!) {recognizedString in 
      print(recognizedString) 
      self.classificationLabel.text = recognizedString 
     } 
    } 


lazy var rectanglesRequest: VNDetectRectanglesRequest = { 
    print("Tony 1 Requested....") 
    return VNDetectRectanglesRequest(completionHandler: self.handleRectangles) 



}() 

func handleRectangles(request: VNRequest, error: Error?) { 
    guard let observations = request.results as? [VNRectangleObservation] 
     else { fatalError("unexpected result type from VNDetectRectanglesRequest") } 
    guard let detectedRectangle = observations.first else { 
     DispatchQueue.main.async { 
      self.classificationLabel.text = "No rectangles detected." 
     } 
     return 
    } 
    let imageSize = inputImage.extent.size 

    // Verify detected rectangle is valid. 
    let boundingBox = detectedRectangle.boundingBox.scaled(to: imageSize) 
    guard inputImage.extent.contains(boundingBox) 
     else { print("invalid detected rectangle"); return } 

    // Rectify the detected image and reduce it to inverted grayscale for applying model. 
    let topLeft = detectedRectangle.topLeft.scaled(to: imageSize) 
    let topRight = detectedRectangle.topRight.scaled(to: imageSize) 
    let bottomLeft = detectedRectangle.bottomLeft.scaled(to: imageSize) 
    let bottomRight = detectedRectangle.bottomRight.scaled(to: imageSize) 
    let correctedImage = inputImage 
     .cropped(to: boundingBox) 
     .applyingFilter("CIPerspectiveCorrection", parameters: [ 
      "inputTopLeft": CIVector(cgPoint: topLeft), 
      "inputTopRight": CIVector(cgPoint: topRight), 
      "inputBottomLeft": CIVector(cgPoint: bottomLeft), 
      "inputBottomRight": CIVector(cgPoint: bottomRight) 
      ]) 
    //   .applyingFilter("CIColorControls", parameters: [ 
    //    kCIInputSaturationKey: 0, 
    //    kCIInputContrastKey: 32 
    //   ]) 


    // Show the pre-processed image 
    DispatchQueue.main.async { 
     self.correctedImageView.image = UIImage(ciImage: correctedImage) 
     if self.correctedImageView.image != nil { 
      print("Tony 2 Handle Rectangle....") 
      print("Tony: Corected image here......") 

     }else { 
      print("Tony: No corected image......") 
     } 

    } 
    print("Tony 3 run OCR....") 
    self.startOCR() 
} 

我也得到一个紫色的错误说的UIImage应该在下面的PIC主线程上使用...

enter image description here

+0

这个错误告诉你应该从主线程使用UIImageView,而不是使用UIImage。 –

回答

0

您需要使用主线程,只要你”重新使用UIImageView或任何其他UIKit类(除非另有说明,例如在后台线程上构建UIImage时)。

您可以使用GCD在主线程上执行此操作。

DispatchQueue.main.async { 
    //Handle UIKit actions here 
} 

来源:Apple Documentation

线程注意事项: 操作,以应用程序的用户界面 必须发生在主线程。因此,您应始终通过在应用程序的主线程 中运行的代码来调用 UIView类的方法。 是创建视图对象本身的唯一时间,但所有其他操作 应发生在主线程上。

+0

我已将调度主队列添加到该函数中,并删除了紫色错误。但在调用具有相同失败功能时仍然失败。 NSAssert(widthOfImage> 0 && heightOfImage> 0,@“传递的图像不能为空 - 它应该至少1px高和宽)”; –

相关问题