照片框架崩溃:'This application is not allowed to access Photo data.'
Photos Framework crash: 'This application is not allowed to access Photo data.'
如果未授予照片访问权限并且请求权限的对象即将解除分配,应用程序会崩溃。
Apple 的示例代码(用于照片框架)也崩溃了。
我的应用崩溃并显示以下堆栈跟踪:
*** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'This application is not allowed to access Photo data.'
*** First throw call stack:
(
0 CoreFoundation 0x000000010a324f65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010c030deb objc_exception_throw + 48
2 PhotoLibraryServices 0x000000011c1f7e2d -[PLPhotoLibrary initWithTransientContext:name:] + 1183
3 Photos 0x000000011bd6a445 __30-[PHPhotoLibrary photoLibrary]_block_invoke + 59
4 libdispatch.dylib 0x0000000110f5e49b _dispatch_client_callout + 8
5 libdispatch.dylib 0x0000000110f49e28 dispatch_once_f + 543
6 Photos 0x000000011bd6a404 -[PHPhotoLibrary photoLibrary] + 140
7 Photos 0x000000011bd3e5e8 -[PHCoreImageManager _cancelAndFlushPreheatItemsForAssets:CPLPrefetching:domain:operation:passingTestHandler:didCancelHandler:] + 174
8 Photos 0x000000011bd5900c -[PHImageManager dealloc] + 176
9 libobjc.A.dylib 0x000000010c044afe _ZN11objc_object17sidetable_releaseEb + 232
10 MyApp 0x000000010f271812 _TToFC12MyApp16PhotosDataSourceE + 66
11 libobjc.A.dylib 0x000000010c02f7bb _ZL27object_cxxDestructFromClassP11objc_objectP10objc_class + 127
12 libobjc.A.dylib 0x000000010c03a390 objc_destructInstance + 93
13 libobjc.A.dylib 0x000000010c03a3c3 object_dispose + 22
14 MyApp 0x000000010f271796 _TFC12MyApp16PhotosDataSourceD + 198
15 MyApp 0x000000010f2717c2 _TToFC12MyApp16PhotosDataSourceD + 34
16 libobjc.A.dylib 0x000000010c044afe _ZN11objc_object17sidetable_releaseEb + 232
17 MyApp 0x000000010f2668f2 _TToFC12MyApp25PhotoPickerViewControllerE + 178
18 libobjc.A.dylib 0x000000010c02f7bb _ZL27object_cxxDestructFromClassP11objc_objectP10objc_class + 127
19 libobjc.A.dylib 0x000000010c03a390 objc_destructInstance + 93
20 libobjc.A.dylib 0x000000010c03a3c3 object_dispose + 22
21 UIKit 0x000000010adb54b9 -[UIResponder dealloc] + 130
22 UIKit 0x000000010ad40721 -[UIViewController dealloc] + 1949
23 UIKit 0x000000010ad1c97b -[UIPresentationController .cxx_destruct] + 262
24 libobjc.A.dylib 0x000000010c02f7bb _ZL27object_cxxDestructFromClassP11objc_objectP10objc_class + 127
25 libobjc.A.dylib 0x000000010c03a390 objc_destructInstance + 93
26 libobjc.A.dylib 0x000000010c03a3c3 object_dispose + 22
27 UIKit 0x000000010ad185db -[UIPresentationController dealloc] + 56
28 UIKit 0x000000010b4a9046 -[_UIFullscreenPresentationController dealloc] + 56
29 libobjc.A.dylib 0x000000010c044afe _ZN11objc_object17sidetable_releaseEb + 232
30 UIKit 0x000000010ad1838c __destroy_helper_block_411 + 24
31 libsystem_blocks.dylib 0x0000000110fdb6b1 _Block_release + 128
32 UIKit 0x000000010b568d9c -[_UIViewControllerTransitionContext .cxx_destruct] + 94
33 libobjc.A.dylib 0x000000010c02f7bb _ZL27object_cxxDestructFromClassP11objc_objectP10objc_class + 127
34 libobjc.A.dylib 0x000000010c03a390 objc_destructInstance + 93
35 libobjc.A.dylib 0x000000010c03a3c3 object_dispose + 22
36 UIKit 0x000000010b567825 -[_UIViewControllerTransitionContext dealloc] + 56
37 UIKit 0x000000010b568e51 -[_UIViewControllerOneToOneTransitionContext dealloc] + 79
38 libobjc.A.dylib 0x000000010c044afe _ZN11objc_object17sidetable_releaseEb + 232
39 MyApp 0x000000010d4818d2 block_destroy_helper + 66
40 MyApp 0x000000010d481988 _TPA__TFFC16MyApp31CustomShrinkAnimator17animateTransitionFS0_FPSo36UIViewControllerContextTransitioning_T_U0_FSbT_ + 152
41 MyApp 0x000000010d481a28 _TPA__TTRXFo_dSb_dT__XFo_iSb_iT__ + 120
42 MyApp 0x000000010d481aed block_destroy_helper8 + 13
43 libsystem_blocks.dylib 0x0000000110fdb6b1 _Block_release + 128
44 UIKit 0x000000010ac5ee47 -[UIViewAnimationBlockDelegate .cxx_destruct] + 43
45 libobjc.A.dylib 0x000000010c02f7bb _ZL27object_cxxDestructFromClassP11objc_objectP10objc_class + 127
46 libobjc.A.dylib 0x000000010c03a390 objc_destructInstance + 93
47 libobjc.A.dylib 0x000000010c03a3c3 object_dispose + 22
48 libobjc.A.dylib 0x000000010c044afe _ZN11objc_object17sidetable_releaseEb + 232
49 CoreFoundation 0x000000010a228d4d -[__NSDictionaryI dealloc] + 141
50 libobjc.A.dylib 0x000000010c044afe _ZN11objc_object17sidetable_releaseEb + 232
51 libobjc.A.dylib 0x000000010c0450b8 _ZN12_GLOBAL__N_119AutoreleasePoolPage3popEPv + 488
52 CoreFoundation 0x000000010a2129c6 _CFAutoreleasePoolPop + 22
53 CoreFoundation 0x000000010a246941 __CFRunLoopRun + 2081
54 CoreFoundation 0x000000010a245e98 CFRunLoopRunSpecific + 488
55 GraphicsServices 0x0000000112598ad2 GSEventRunModal + 161
56 UIKit 0x000000010abb8676 UIApplicationMain + 171
57 MyApp 0x00000001082b470d main + 109
58 libdyld.dylib 0x0000000110f9392d start + 1
59 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
任何指点将不胜感激。
这是预期的行为。
在您使用照片框架的任何方法(如获取、更改)之前,请检查照片库的权限状态。这是通过调用 PHPhotoLibrary
的 authorizationStatus
方法来完成的。如果此方法 PHAuthorizationStatusNotDetermined
,请使用 requestAuthorization
方法请求许可。如果 authorizationStatus
的值 PHAuthorizationStatusDenied
优雅地下降。
要点是让 requestAuthorization
提示访问照片库而不是直接获取方法。
我也有过同样的经历。但是,要重现退出视图控制器并释放它是不够的,还需要在模拟器中模拟内存警告。这迫使崩溃。
所以,我的解决方案是不使用 PHImageManager 作为全局变量,而是在需要时访问它。而不是这个:
private let imageManager = PHImageManager.defaultManager()
...
imageManager.requestImageForAsset(....)
我在需要的时候喜欢这个:
PHImageManager.defaultManager().requestImageForAsset(....)
我的崩溃情况是使用 PHCachingImageManager 作为 属性。
fileprivate let imageManager = PHCachingImageManager()
我通过将其更改为惰性变量来修复它:
fileprivate lazy var imageManager = PHCachingImageManager()
PHImageManager
在初始化时添加调度源事件 DISPATCH_SOURCE_TYPE_MEMORYPRESSURE。
当收到内存警告时,Photos 框架将尝试删除此事件处理程序中的缓存数据。届时,如果你有调用但没有获得授权,会导致崩溃
导致崩溃
对我来说,如果用户未授予照片权限,它会在 11 和 12 版本上崩溃,但在 iOS13 上工作正常。下面的行导致了问题。
fileprivate var imageManager = PHImageManager() // Crash
fileprivate lazy var imageManager = PHImageManager() // fixed the crash.
如果未授予照片访问权限并且请求权限的对象即将解除分配,应用程序会崩溃。
Apple 的示例代码(用于照片框架)也崩溃了。
我的应用崩溃并显示以下堆栈跟踪:
*** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'This application is not allowed to access Photo data.' *** First throw call stack: ( 0 CoreFoundation 0x000000010a324f65 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010c030deb objc_exception_throw + 48 2 PhotoLibraryServices 0x000000011c1f7e2d -[PLPhotoLibrary initWithTransientContext:name:] + 1183 3 Photos 0x000000011bd6a445 __30-[PHPhotoLibrary photoLibrary]_block_invoke + 59 4 libdispatch.dylib 0x0000000110f5e49b _dispatch_client_callout + 8 5 libdispatch.dylib 0x0000000110f49e28 dispatch_once_f + 543 6 Photos 0x000000011bd6a404 -[PHPhotoLibrary photoLibrary] + 140 7 Photos 0x000000011bd3e5e8 -[PHCoreImageManager _cancelAndFlushPreheatItemsForAssets:CPLPrefetching:domain:operation:passingTestHandler:didCancelHandler:] + 174 8 Photos 0x000000011bd5900c -[PHImageManager dealloc] + 176 9 libobjc.A.dylib 0x000000010c044afe _ZN11objc_object17sidetable_releaseEb + 232 10 MyApp 0x000000010f271812 _TToFC12MyApp16PhotosDataSourceE + 66 11 libobjc.A.dylib 0x000000010c02f7bb _ZL27object_cxxDestructFromClassP11objc_objectP10objc_class + 127 12 libobjc.A.dylib 0x000000010c03a390 objc_destructInstance + 93 13 libobjc.A.dylib 0x000000010c03a3c3 object_dispose + 22 14 MyApp 0x000000010f271796 _TFC12MyApp16PhotosDataSourceD + 198 15 MyApp 0x000000010f2717c2 _TToFC12MyApp16PhotosDataSourceD + 34 16 libobjc.A.dylib 0x000000010c044afe _ZN11objc_object17sidetable_releaseEb + 232 17 MyApp 0x000000010f2668f2 _TToFC12MyApp25PhotoPickerViewControllerE + 178 18 libobjc.A.dylib 0x000000010c02f7bb _ZL27object_cxxDestructFromClassP11objc_objectP10objc_class + 127 19 libobjc.A.dylib 0x000000010c03a390 objc_destructInstance + 93 20 libobjc.A.dylib 0x000000010c03a3c3 object_dispose + 22 21 UIKit 0x000000010adb54b9 -[UIResponder dealloc] + 130 22 UIKit 0x000000010ad40721 -[UIViewController dealloc] + 1949 23 UIKit 0x000000010ad1c97b -[UIPresentationController .cxx_destruct] + 262 24 libobjc.A.dylib 0x000000010c02f7bb _ZL27object_cxxDestructFromClassP11objc_objectP10objc_class + 127 25 libobjc.A.dylib 0x000000010c03a390 objc_destructInstance + 93 26 libobjc.A.dylib 0x000000010c03a3c3 object_dispose + 22 27 UIKit 0x000000010ad185db -[UIPresentationController dealloc] + 56 28 UIKit 0x000000010b4a9046 -[_UIFullscreenPresentationController dealloc] + 56 29 libobjc.A.dylib 0x000000010c044afe _ZN11objc_object17sidetable_releaseEb + 232 30 UIKit 0x000000010ad1838c __destroy_helper_block_411 + 24 31 libsystem_blocks.dylib 0x0000000110fdb6b1 _Block_release + 128 32 UIKit 0x000000010b568d9c -[_UIViewControllerTransitionContext .cxx_destruct] + 94 33 libobjc.A.dylib 0x000000010c02f7bb _ZL27object_cxxDestructFromClassP11objc_objectP10objc_class + 127 34 libobjc.A.dylib 0x000000010c03a390 objc_destructInstance + 93 35 libobjc.A.dylib 0x000000010c03a3c3 object_dispose + 22 36 UIKit 0x000000010b567825 -[_UIViewControllerTransitionContext dealloc] + 56 37 UIKit 0x000000010b568e51 -[_UIViewControllerOneToOneTransitionContext dealloc] + 79 38 libobjc.A.dylib 0x000000010c044afe _ZN11objc_object17sidetable_releaseEb + 232 39 MyApp 0x000000010d4818d2 block_destroy_helper + 66 40 MyApp 0x000000010d481988 _TPA__TFFC16MyApp31CustomShrinkAnimator17animateTransitionFS0_FPSo36UIViewControllerContextTransitioning_T_U0_FSbT_ + 152 41 MyApp 0x000000010d481a28 _TPA__TTRXFo_dSb_dT__XFo_iSb_iT__ + 120 42 MyApp 0x000000010d481aed block_destroy_helper8 + 13 43 libsystem_blocks.dylib 0x0000000110fdb6b1 _Block_release + 128 44 UIKit 0x000000010ac5ee47 -[UIViewAnimationBlockDelegate .cxx_destruct] + 43 45 libobjc.A.dylib 0x000000010c02f7bb _ZL27object_cxxDestructFromClassP11objc_objectP10objc_class + 127 46 libobjc.A.dylib 0x000000010c03a390 objc_destructInstance + 93 47 libobjc.A.dylib 0x000000010c03a3c3 object_dispose + 22 48 libobjc.A.dylib 0x000000010c044afe _ZN11objc_object17sidetable_releaseEb + 232 49 CoreFoundation 0x000000010a228d4d -[__NSDictionaryI dealloc] + 141 50 libobjc.A.dylib 0x000000010c044afe _ZN11objc_object17sidetable_releaseEb + 232 51 libobjc.A.dylib 0x000000010c0450b8 _ZN12_GLOBAL__N_119AutoreleasePoolPage3popEPv + 488 52 CoreFoundation 0x000000010a2129c6 _CFAutoreleasePoolPop + 22 53 CoreFoundation 0x000000010a246941 __CFRunLoopRun + 2081 54 CoreFoundation 0x000000010a245e98 CFRunLoopRunSpecific + 488 55 GraphicsServices 0x0000000112598ad2 GSEventRunModal + 161 56 UIKit 0x000000010abb8676 UIApplicationMain + 171 57 MyApp 0x00000001082b470d main + 109 58 libdyld.dylib 0x0000000110f9392d start + 1 59 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException
任何指点将不胜感激。
这是预期的行为。
在您使用照片框架的任何方法(如获取、更改)之前,请检查照片库的权限状态。这是通过调用 PHPhotoLibrary
的 authorizationStatus
方法来完成的。如果此方法 PHAuthorizationStatusNotDetermined
,请使用 requestAuthorization
方法请求许可。如果 authorizationStatus
的值 PHAuthorizationStatusDenied
优雅地下降。
要点是让 requestAuthorization
提示访问照片库而不是直接获取方法。
我也有过同样的经历。但是,要重现退出视图控制器并释放它是不够的,还需要在模拟器中模拟内存警告。这迫使崩溃。
所以,我的解决方案是不使用 PHImageManager 作为全局变量,而是在需要时访问它。而不是这个:
private let imageManager = PHImageManager.defaultManager()
...
imageManager.requestImageForAsset(....)
我在需要的时候喜欢这个:
PHImageManager.defaultManager().requestImageForAsset(....)
我的崩溃情况是使用 PHCachingImageManager 作为 属性。
fileprivate let imageManager = PHCachingImageManager()
我通过将其更改为惰性变量来修复它:
fileprivate lazy var imageManager = PHCachingImageManager()
PHImageManager
在初始化时添加调度源事件 DISPATCH_SOURCE_TYPE_MEMORYPRESSURE。
当收到内存警告时,Photos 框架将尝试删除此事件处理程序中的缓存数据。届时,如果你有调用但没有获得授权,会导致崩溃
导致崩溃
对我来说,如果用户未授予照片权限,它会在 11 和 12 版本上崩溃,但在 iOS13 上工作正常。下面的行导致了问题。
fileprivate var imageManager = PHImageManager() // Crash
fileprivate lazy var imageManager = PHImageManager() // fixed the crash.