0

我正在使用以下代码在我的应用程序启动时请求访问用户照片库。

@IBAction func requestPhotoLibraryAccess(sender: UIButton) {
    PHPhotoLibrary .requestAuthorization { (status) -> Void in
        if (status == PHAuthorizationStatus.Authorized) {
            self.showViewController(HomeViewController(), sender: self);
        }
    }
}

我不明白的是,当我打电话时

self.showViewController(HomeViewController(), sender: self); 

它抛出

2015-10-04 20:21:13.520 Bokeh[11284:1352415] This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.  This will cause an exception in a future release.
Stack:(
  0   CoreFoundation                      0x000000010b48ef65 __exceptionPreprocess + 165
  1   libobjc.A.dylib                     0x000000010d249deb objc_exception_throw + 48
  2   CoreFoundation                      0x000000010b48ee9d +[NSException raise:format:] + 205
  3   Foundation                          0x000000010b9ff3b5 _AssertAutolayoutOnMainThreadOnly + 79
  4   Foundation                          0x000000010b8601be -[NSISEngine withBehaviors:performModifications:] + 31
  5   UIKit                               0x000000010c653637 -[UIView(AdditionalLayoutSupport) _withAutomaticEngineOptimizationDisabledIfEngineExists:] + 58
  6   UIKit                               0x000000010c653fb9 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:] + 154
  7   UIKit                               0x000000010c653c29 -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:] + 309
  8   UIKit                               0x000000010c653fe5 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:] + 198
  9   UIKit                               0x000000010c653c29 -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:] + 309
  10  UIKit                               0x000000010c653fe5 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:forSecondPass:] + 198
  11  UIKit                               0x000000010c65461d __60-[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded]_block_invoke + 98
  12  UIKit                               0x000000010c653640 -[UIView(AdditionalLayoutSupport) _withAutomaticEngineOptimizationDisabledIfEngineExists:] + 67
  13  UIKit                               0x000000010c65417a -[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded] + 254
  14  UIKit                               0x000000010c654e8f -[UIView(AdditionalLayoutSupport) _updateConstraintsAtEngineLevelIfNeeded] + 272
  15  UIKit                               0x000000010be7360c -[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 159
  16  UIKit                               0x000000010be83143 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 749
  17  QuartzCore                          0x000000011267a36a -[CALayer layoutSublayers] + 146
  18  QuartzCore                          0x000000011266ebd0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
  19  QuartzCore                          0x000000011266ea4e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
  20  QuartzCore                          0x00000001126631d5 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
  21  QuartzCore                          0x00000001126909f0 _ZN2CA11Transaction6commitEv + 508
  22  QuartzCore                          0x0000000112690cd4 _ZN2CA11Transaction14release_threadEPv + 224
  23  libsystem_pthread.dylib             0x000000010e09939c _pthread_tsd_cleanup + 470
  24  libsystem_pthread.dylib             0x000000010e098f78 _pthread_exit + 117
  25  libsystem_pthread.dylib             0x000000010e097596 pthread_attr_getschedpolicy + 0
  26  libsystem_pthread.dylib             0x000000010e095375 start_wqthread + 13
)

如果我将调用移出回调,它可以正常工作。

Apple 文档没有说明它是在后台线程上运行的,尽管我认为这就是回调的全部意义所在。

此方法总是立即返回。如果用户先前已授予或拒绝照片库访问权限,则在调用时执行处理程序块;否则,它会显示警报并仅在用户响应警报后才执行该块。

我是否必须将导航编组回 UI 线程?

4

2 回答 2

3

尝试

dispatch_async(dispatch_get_main_queue(),{

    self.showViewController(HomeViewController(), sender: self);

 })

代替

self.showViewController(HomeViewController(), sender: self);
于 2015-10-05T03:43:33.940 回答
0

我有类似的堆栈,因为忘记将NSPhotoLibraryUsageDescription带有字符串值的键添加到Info.plist文件中。

于 2017-02-10T10:19:22.587 回答