【问题标题】:Showing a View Controller within a Photos Access request callback throws exception在照片访问请求回调中显示视图控制器会引发异常
【发布时间】:2015-10-05 03:25:30
【问题描述】:

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

@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 线程?

【问题讨论】:

    标签: ios swift photokit


    【解决方案1】:

    试试

    dispatch_async(dispatch_get_main_queue(),{
    
        self.showViewController(HomeViewController(), sender: self);
    
     })
    

    代替

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

    【讨论】:

    • 这就是我最终所做的。我在另一篇文章中读到,所有闭包都应被视为在后台线程上运行,因此如果您与之交互,请将所有内容编组回 UI。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      相关资源
      最近更新 更多