【发布时间】:2019-10-28 07:51:51
【问题描述】:
我对 Swift 很陌生 - 在执行在 PDFView 中显示文档的简单任务时遇到了一些麻烦。我没有使用 Storyboards 或 IBOutlets 等。知道为什么下面会崩溃吗?
我尝试引用外部链接,但它给了我同样的错误。 PDF 存在于 bundle 目录中。
导入 UIKit 导入 PDFKit
类视图控制器:UIViewController {
private var pdfView = PDFView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let myNewView = PDFView(frame: CGRect(x: 10, y: 100, width: 500, height: 500))
// Add border to UIView
myNewView.layer.borderWidth=2
// Change UIView Border Color to Red
myNewView.layer.borderColor = UIColor.red.cgColor
guard let path = Bundle.main.path(forResource: "CV", ofType: "pdf") else { return }
let url = URL(fileURLWithPath: path)
let document = PDFDocument(url: url)
myNewView.document = document
// Add UIView as a Subview
self.view.addSubview(myNewView)
}
}
构建成功。但我得到了以下内容。
2019-06-13 17:49:35.702174+0100 Appp[81235:19343962] * 由于未捕获的异常“CALayerInvalidGeometry”而终止应用程序,原因:“CALayer 位置包含 NaN:[nan nan]” * 首先抛出调用栈: ( 0 核心基础 0x00000001083446fb 异常预处理 + 331 1 libobjc.A.dylib 0x0000000106250ac5 objc_exception_throw + 48 2 核心基础 0x0000000108344555 + [NSException raise:format:] + 197 3 石英核心 0x000000010b4072ae _ZN2CA5Layer12set_positionERKNS_4Vec2IdEEb + 140 4 石英核心 0x000000010b3f668b -[CALayer setPosition:] + 57 5 石英核心 0x000000010b3f6de3 -[CALayer setFrame:] + 560 6 PDFKit 0x0000000107135097-[PDFPageLayerTile initWithFrame:forPageLayer:withRenderingTransform:tileContentsScale:generationID:] + 168 7 PDFKit 0x000000010713a805-[PDFPageLayer _updateTiles] + 3439 8 PDFKit 0x0000000107135d8e -[PDFPageLayer setNeedsTilesUpdate] + 87 9 PDFKit 0x000000010714a97d-[PDFPageView setNeedsTilesUpdate] + 48 10 PDFKit 0x000000010714c816-[PDFPageView setFrame:] + 334 11 PDFKit 0x000000010714311c-[PDFDocumentView createPageViewForPageAtIndex:] + 764 12 PDFKit 0x0000000107144326 - [PDF文档视图更新可见性] + 1726 13 PDFKit 0x00000001071b376f -[PDFView resizeDisplayView:] + 517 14 PDFKit 0x00000001071accdf -[PDFView layoutDocumentView] + 464 15 PDFKit 0x00000001071a836a-[PDFView setDocument:waitDuration:] + 1246 16 应用程序 0x000000010596c4d1 $s7Appp14ViewControllerC11viewDidLoadyyF + 1441 17 应用程序 0x000000010596c7b4 $s7Appp14ViewControllerC11viewDidLoadyyFTo + 36 18 UIKitCore 0x000000010d0ec43b -[UIViewController loadViewIfRequired] + 1183 19 UIKitCore 0x000000010d0ec868 -[UIViewController 视图] + 27 20 UIKitCore 0x000000010d724c33-[UIWindow addRootViewControllerViewIfPossible] + 122 21 UIKitCore 0x000000010d725327 -[UIWindow _setHidden:forced:] + 289 22 UIKitCore 0x000000010d737f86 -[UIWindow makeKeyAndVisible] + 42 23 UIKitCore 0x000000010d6e7f1c-[UIApplication_callInitializationDelegatesForMainScene:transitionContext:] + 4555 24 UIKitCore 0x000000010d6ed0c6-[UIApplication_runWithMainScene:transitionContext:completion:] + 1617 25 UIKitCore 0x000000010cf326d6 __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 904 26 UIKitCore 0x000000010cf3afce + [_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153 27 UIKitCore 0x000000010cf322ec -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 236 28 UIKitCore 0x000000010cf32c48 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 1091 29 UIKitCore 0x000000010cf30fba __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 782 30 UIKitCore 0x000000010cf30c71-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 433 31 UIKitCore 0x000000010cf359b6 __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 576 32 UIKitCore 0x000000010cf36610 _performActionsWithDelayForTransitionContext + 100 33 UIKitCore 0x000000010cf3571d -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 223 34 UIKitCore 0x000000010cf3a6d0 -[_UICanvas 场景:didUpdateWithDiff:transitionContext:completion:] + 392 35 UIKitCore 0x000000010d6eb9a8 -[UIApplication 工作区:didCreateScene:withTransitionContext:completion:] + 514 36 UIKitCore 0x000000010d2a2dfa-[UIApplicationSceneClientAgent 场景:didInitializeWithEvent:完成:] + 361 37 前板服务 0x00000001135df125 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 448 38 FrontBoardServices 0x00000001135e8ed6 __56-[FBSWorkspace 客户端:handleCreateScene:withCompletion:]_block_invoke_2 + 283 39 前板服务 0x00000001135e8700 __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 53 40 libdispatch.dylib 0x00000001096d7db5 _dispatch_client_callout + 8 41 libdispatch.dylib 0x00000001096db2ba _dispatch_block_invoke_direct + 300 42 前板服务 0x000000011361a146 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK + 30 43 前板服务 0x0000000113619dfe -[FBSSerialQueue _performNext] + 451 44 前板服务 0x000000011361a393 -[FBSSerialQueue _performNextFromRunLoopSource] + 42 45 核心基础 0x00000001082abbe1 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 46 核心基础 0x00000001082ab463 __CFRunLoopDoSources0 + 243 47 核心基础 0x00000001082a5b1f __CFRunLoopRun + 1231 48 核心基础 0x00000001082a5302 CFRunLoopRunSpecific + 626 49 图形服务 0x00000001107c62fe GSEventRunModal + 65 50 UIKitCore 0x000000010d6eeba2 UIApplicationMain + 140 51 应用程序 0x000000010596f68b 主 + 75 52 libdyld.dylib 0x000000010974c541 开始 + 1 53 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止 (lldb)
【问题讨论】:
-
您的代码没有问题。我想知道PDF文件是否以某种方式损坏?或许可以尝试另一个 PDF 文件来确定。
标签: swift