我试图获取显示图像的尺寸,以在我使用苹果的 Vision 框架识别的文本上绘制边界框。因此,我在按下此功能的按钮后运行 VNRecognizeTextRequest
func readImage(image:NSImage, completionHandler:@escaping(([VNRecognizedText]?,Error?)->()), comp:@escaping((Double?,Error?)->())) {
var recognizedTexts = [VNRecognizedText]()
var rr = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
let requestHandler = VNImageRequestHandler(cgImage: image.cgImage(forProposedRect: &rr, context: nil, hints: nil)!
, 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 …Run Code Online (Sandbox Code Playgroud) 我想进行单元测试以查看视图控制器是否正在呈现另一个视图控制器。
func testMainTabController_WhenActionButtonIsTapped_NewSuppliersInvoiceControllerIsCreatedV2() {
let sut = MainTabBarController()
sut.loadViewIfNeeded()
let myExpectation = expectation(description: "The sut should present a view controller")
sut.actionButton.sendActions(for: .touchUpInside)
if let x = sut.presentedViewController {
myExpectation.fulfill()
} else {
XCTFail("The view controller was not presented")
}
wait(for: [myExpectation], timeout: 3)
}
Run Code Online (Sandbox Code Playgroud)
这里我得到的结果是测试失败。因为我得到 nil 作为presentedViewController。这是按钮的代码
@objc func handleActionButtonTap() {
let suppliersInvoiceController = SuppliersInvoiceController()
let navCon = UINavigationController(rootViewController: suppliersInvoiceController)
navCon.modalPresentationStyle = .fullScreen
present(navCon, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
这是我在测试中写的代码。当我运行单元测试并调用当前方法时,按钮中的代码被成功调用。当然,如果我运行该应用程序,它就可以正常工作。当我点击按钮时,我会得到呈现的视图控制器。如果我在handleActionButtonTap()和print(vc)中输入let vc =presentedViewController,我就会得到nav con。但为什么我不能在单元测试中做到这一点?
有人知道发生了什么事吗?
谢谢
我正在构建一个机器学习应用程序来读取 pdf 信息,为了训练我的算法,我需要获取 pdf 注释位置。有没有办法设置手势识别器并在单击时将注释添加到数组中?我成功地将注释添加到正则表达式的 pdf 中。但我需要添加注释及其相关信息(单击后在文档中的位置)我可以向应用程序添加手势识别器吗?我的应用程序使用 SwiftUI。
func makeNSView(context: NSViewRepresentableContext<PDFViewRepresentedView>) -> PDFViewRepresentedView.NSViewType {
let pdfView = PDFView()
let document = PDFDocument(url: url)
let regex = try! NSRegularExpression(pattern: #"[0-9.,]+(,|\.)\d\d"#, options: .caseInsensitive)
let string = document?.string!
let results = regex.matches(in: string!, options: .withoutAnchoringBounds, range: NSRange(0..<(string?.utf16.count)!))
let page = document?.page(at: 0)!
results.forEach { (result) in
let startIndex = result.range.location
let endIndex = result.range.location + result.range.length - 1
let selection = document?.selection(from: page!, atCharacterIndex: startIndex, to: page!, atCharacterIndex: endIndex)
print(selection!.bounds(for: page!))
let pdfAnnotation …Run Code Online (Sandbox Code Playgroud) 我是初学者程序员,我的目标是OSX.我想创建一个对AppDelegate内部的托管对象上下文属性的引用,以便在我正在创建的Core Data项目中使用它
我试试
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let aVariable = appDelegate.someVariable
Run Code Online (Sandbox Code Playgroud)
或使其变为可变,输入var而不是let.请问有人可以给我一些帮助吗?
谢谢
swift ×4
ios ×2
swiftui ×2
xcode ×2
apple-vision ×1
cocoa ×1
core-data ×1
pdfkit ×1
unit-testing ×1