当另一个结束时运行一个函数,iOS,Swift

时间:2017-10-05 21:02:51

标签: ios swift function completion

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

这是我从ocr得到的错误。

  

NSAssert(widthOfImage> 0&& heightOfImage> 0,@"传递的图像不能为空 - 它应至少为1px高和宽");

下面是我的控制台读数,我放置了打印件以查看流程。

  

托尼1请求....   托尼3运行OCR ....   托尼2把手矩形....   托尼:这里的核心形象......   (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应该用在下面图片中的主线程上......

enter image description here

1 个答案:

答案 0 :(得分:0)

每当您使用UIImageView或任何其他UIKit类时,您都需要使用主线程(除非另有说明,例如在后台线程上构建UIImages时)。

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

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

来源:Apple Documentation

  

线程注意事项:   对应用程序用户的操作   接口必须出现在主线程上。因此,你应该经常打电话   从主线程中运行的代码中获取UIView类的方法   你的申请。这可能不是绝对必要的唯一时间   是在创建视图对象本身时,还有所有其他操作   应该发生在主线程上。

相关问题