Col*_*las 14 snapshot objective-c parent-child uiviewcontroller ios
childVC到parentVCchildVC.view为什么?
我刚刚遇到一个奇怪的行为,我想知道它是否正常或是否是一个bug.
我有一个视图控制器childVC,它是一个孩子parentVC.创建父/子关系时,我的代码是
[parentVC addChildViewController:childVC] ;
[parentVC.view addSubview:childVC.view] ;
[childVC didMoveToParentViewController: parentVC] ;
Run Code Online (Sandbox Code Playgroud)
几行,我想创建一个快照childVC.view.我的代码是
UIView * view = childVC.view ;
UIGraphicsBeginImageContextWithOptions(view.contentSize, NO, 0);
{
[...]
[view drawViewHierarchyInRect:view.bounds
afterScreenUpdates:YES];
image = UIGraphicsGetImageFromCurrentImageContext();
[...]
}
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
然后我有错误:
*由于未捕获的异常'UIViewControllerHierarchyInconsistency'终止应用程序,原因:'子视图控制器:应该有父视图控制器:( null)但实际的父级是:'*第一次抛出调用堆栈:(0 CoreFoundation 0x0260b466 __exceptionPreprocess + 182 1 libobjc.A .dylib
0x02290a97 objc_exception_throw + 44 2 CoreFoundation
0x0260b38d + [NSException raise:format:] + 141 3 UIKit
0x01136710 - [UIView(Hierarchy)_associatedViewControllerForwardsAppearanceCallbacks:performHierarchyCheck:isRoot:] + 352 4 UIKit 0x01136b13 - [UIView(Hierarchy)_willMoveToWindow:withAncestorView: ] + 285 5 UIKit 0x0114330a - [UIView(内部)_addSubview:定位:relativeTo:] + 511 6 UIKit 0x01136252 - [UIView(Hierarchy)addSubview:] + 56 7 UIKit
0x0114ab0e + [_ UIReplicantView _pendingSnapshotOfTarget:snapshotBlock:] + 584 8 UIKit 0x011312fe - [UIView drawViewHierarchyInRect:afterScreenUpdates:] + 287
为什么会这样?我可以解决这个问题吗?
实际上,childVC.view是一个UIScrollView和快照的代码是
UIScrollView * scrollView = (UIScrollView *)childVC.view;
UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, NO, 0);
{
CGPoint savedContentOffset = scrollView.contentOffset;
CGRect savedFrame = scrollView.frame;
scrollView.contentOffset = CGPointZero;
scrollView.frame = CGRectMake(0,
0,
scrollView.contentSize.width,
scrollView.contentSize.height);
[scrollView drawViewHierarchyInRect:scrollView.bounds
afterScreenUpdates:YES];
image = UIGraphicsGetImageFromCurrentImageContext();
scrollView.contentOffset = savedContentOffset;
scrollView.frame = savedFrame;
}
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
也许这会产生很大的不同.
| 归档时间: |
|
| 查看次数: |
758 次 |
| 最近记录: |