gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:06:44 26 4
gpt4 key购买 nike

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

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

NSAssert( widthOfImage > 0 && heightOfImage > 0, @"Passed image must not be empty - it should be at least 1px tall and wide");

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

Tony 1 Requested.... Tony 3 run OCR.... Tony 2 Handle Rectangle.... Tony: Corected image here...... (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

最佳答案

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

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

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

来源:Apple Documentation

Threading Considerations: Manipulations to your application’s user interface must occur on the main thread. Thus, you should always call the methods of the UIView class from code running in the main thread of your application. The only time this may not be strictly necessary is when creating the view object itself, but all other manipulations should occur on the main thread.

关于ios - 当另一个函数结束时运行一个函数,iOS,Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46594744/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com