【问题标题】:Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan nan]'由于未捕获的异常“CALayerInvalidGeometry”而终止应用程序,原因:“CALayer 位置包含 NaN:[nan nan]”
【发布时间】:2019-10-06 18:05:28
【问题描述】:

我猜这个问题始于XCode 10.2.x 虽然它与XCode 10.1 一起工作得很好

崩溃点是以下代码中的“pdfView.frame = self.view.frame”:

import UIKit
import PDFKit

class ViewController: UIViewController {
    var pdfView = PDFView()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Prepare document to show
        let url = Bundle.main.url(forResource: "sample", withExtension: "pdf")
        let pdfDocument = PDFDocument(url: url!)

        // PDFView
        pdfView = PDFView()
        pdfView.frame = self.view.frame
        pdfView.autoScales = true
        pdfView.document = pdfDocument

        // Add PDFView to viewController
        self.view.addSubview(pdfView)

    }
}

崩溃日志:

2019-05-20 16:24:39.269219+0530 PDFKitDemo[6795:305736] *** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan nan]'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001118c06fb __exceptionPreprocess + 331
    1   libobjc.A.dylib                     0x000000010fd63ac5 objc_exception_throw + 48
    2   CoreFoundation                      0x00000001118c0555 +[NSException raise:format:] + 197
    3   QuartzCore                          0x00000001149632ae _ZN2CA5Layer12set_positionERKNS_4Vec2IdEEb + 140
    4   QuartzCore                          0x000000011495268b -[CALayer setPosition:] + 57
    5   QuartzCore                          0x0000000114952de3 -[CALayer setFrame:] + 560
    6   PDFKit                              0x00000001106c7097 -[PDFPageLayerTile initWithFrame:forPageLayer:withRenderingTransform:tileContentsScale:generationID:] + 168
    7   PDFKit                              0x00000001106cc805 -[PDFPageLayer _updateTiles] + 3439
    8   PDFKit                              0x00000001106c7d8e -[PDFPageLayer setNeedsTilesUpdate] + 87
    9   PDFKit                              0x00000001106dc97d -[PDFPageView setNeedsTilesUpdate] + 48
    10  PDFKit                              0x00000001106de816 -[PDFPageView setFrame:] + 334
    11  PDFKit                              0x00000001106d511c -[PDFDocumentView createPageViewForPageAtIndex:] + 764
    12  PDFKit                              0x00000001106d6326 -[PDFDocumentView updateVisibility] + 1726
    13  PDFKit                              0x000000011074576f -[PDFView resizeDisplayView:] + 517
    14  PDFKit                              0x000000011073ecdf -[PDFView layoutDocumentView] + 464
    15  PDFKit                              0x000000011073a36a -[PDFView setDocument:waitDuration:] + 1246
    16  PDFKitDemo                          0x000000010f4833b3 $s10PDFKitDemo14ViewControllerC11viewDidLoadyyF + 2147
    17  PDFKitDemo                          0x000000010f483874 $s10PDFKitDemo14ViewControllerC11viewDidLoadyyFTo + 36
    18  UIKitCore                           0x000000011666843b -[UIViewController loadViewIfRequired] + 1183
    19  UIKitCore                           0x0000000116668868 -[UIViewController view] + 27
    20  UIKitCore                           0x0000000116ca0c33 -[UIWindow addRootViewControllerViewIfPossible] + 122
    21  UIKitCore                           0x0000000116ca1327 -[UIWindow _setHidden:forced:] + 289
    22  UIKitCore                           0x0000000116cb3f86 -[UIWindow makeKeyAndVisible] + 42
    23  UIKitCore                           0x0000000116c63f1c -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4555
    24  UIKitCore                           0x0000000116c690c6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1617
    25  UIKitCore                           0x00000001164ae6d6 __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 904
    26  UIKitCore                           0x00000001164b6fce +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153
    27  UIKitCore                           0x00000001164ae2ec -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 236
    28  UIKitCore                           0x00000001164aec48 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 1091
    29  UIKitCore                           0x00000001164acfba __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 782
    30  UIKitCore                           0x00000001164acc71 -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 433
    31  UIKitCore                           0x00000001164b19b6 __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 576
    32  UIKitCore                           0x00000001164b2610 _performActionsWithDelayForTransitionContext + 100
    33  UIKitCore                           0x00000001164b171d -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 223
    34  UIKitCore                           0x00000001164b66d0 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392
    35  UIKitCore                           0x0000000116c679a8 -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 514
    36  UIKitCore                           0x000000011681edfa -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 361
    37  FrontBoardServices                  0x000000011cb5b125 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 448
    38  FrontBoardServices                  0x000000011cb64ed6 __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 283
    39  FrontBoardServices                  0x000000011cb64700 __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 53
    40  libdispatch.dylib                   0x0000000112c53db5 _dispatch_client_callout + 8
    41  libdispatch.dylib                   0x0000000112c572ba _dispatch_block_invoke_direct + 300
    42  FrontBoardServices                  0x000000011cb96146 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 30
    43  FrontBoardServices                  0x000000011cb95dfe -[FBSSerialQueue _performNext] + 451
    44  FrontBoardServices                  0x000000011cb96393 -[FBSSerialQueue _performNextFromRunLoopSource] + 42
    45  CoreFoundation                      0x0000000111827be1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    46  CoreFoundation                      0x0000000111827463 __CFRunLoopDoSources0 + 243
    47  CoreFoundation                      0x0000000111821b1f __CFRunLoopRun + 1231
    48  CoreFoundation                      0x0000000111821302 CFRunLoopRunSpecific + 626
    49  GraphicsServices                    0x0000000119d422fe GSEventRunModal + 65
    50  UIKitCore                           0x0000000116c6aba2 UIApplicationMain + 140
    51  PDFKitDemo                          0x000000010f484a6b main + 75
    52  libdyld.dylib                       0x0000000112cc8541 start + 1
    53  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

【问题讨论】:

  • 当我运行它时,崩溃点是“pdfView.document = pdfDocument”,因此它似乎是:stackoverflow.com/search?tab=newest&q=pdfview%20documentstackoverflow.com/questions/55362761/… 的副本。如果您将“pdfView.frame = self.view.frame”移动到“pdfView.document = pdfDocument”下方,则前者确实会成为崩溃点。只要尚未设置框架,似乎在 viewDidLayoutSubviews 之前设置文档就可以了。在我的代码中移动“pdfView.document = pdfDocument”非常麻烦,非常感谢这个副本。

标签: cgrectmake xcode10.2 ios-pdfkit apple-pdfkit


【解决方案1】:

不要具体原因,但是通过添加方法viewDidLayoutSubviews,它修复了崩溃问题。

现在,工作代码是

 override func viewDidLoad() {
        super.viewDidLoad()

        // Prepare document to show
        let url = Bundle.main.url(forResource: "sample", withExtension: "pdf")
        let pdfDocument = PDFDocument(url: url!)


        // PDFView
        pdfView = PDFView()
        pdfView.autoScales = true
        pdfView.document = pdfDocument

        // Add PDFView to viewController
        self.view.addSubview(pdfView)

    }
    override func viewDidLayoutSubviews() {
        pdfView.frame = self.view.frame
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    相关资源
    最近更新 更多