关于iOS的due to uncaught exception 'CALayerInvalidGeometry'错误

来源:互联网 发布:双系统安装教程linux 编辑:程序博客网 时间:2024/06/06 01:54

在iOS开发中很多时候会遇到一些不固定且难以摸透的错误,下面就来解释一下今天遇到的一个错误处理方法,错误代码如下:

2016-03-25 23:42:30.750 YXB[474:4168] *** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer bounds contains NaN: [nan 0; nan 15]'*** First throw call stack:(0   CoreFoundation                      0x000000010bdc1e65 __exceptionPreprocess + 1651   libobjc.A.dylib                     0x000000010b337deb objc_exception_throw + 482   CoreFoundation                      0x000000010bdc1d9d +[NSException raise:format:] + 2053   QuartzCore                          0x000000010910e718 _ZN2CA5Layer10set_boundsERKNS_4RectEb + 2264   QuartzCore                          0x000000010910e9a9 -[CALayer setBounds:] + 1545   UIKit                               0x0000000109424eef -[UIView _createLayerWithFrame:] + 6676   UIKit                               0x00000001094254a8 UIViewCommonInitWithFrame + 7147   UIKit                               0x0000000109425184 -[UIView initWithFrame:] + 1258   UIKit                               0x000000010959db66 -[UIImageView initWithFrame:] + 759   YXB                                 0x0000000107d9bd2b -[QiXinTableViewCell initWithStyle:reuseIdentifier:] + 341910  UIKit                               0x00000001094dc5cb -[UITableView _dequeueReusableViewOfType:withIdentifier:] + 64011  YXB                                 0x0000000107d5c5ad -[InformationView tableView:cellForRowAtIndexPath:] + 10912  UIKit                               0x00000001094efe43 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 76613  UIKit                               0x00000001094eff7b -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 7414  UIKit                               0x00000001094c4a39 -[UITableView _updateVisibleCellsNow:isRecursive:] + 299615  UIKit                               0x00000001094f901c -[UITableView _performWithCachedTraitCollection:] + 9216  UIKit                               0x00000001094dfedc -[UITableView layoutSubviews] + 22417  UIKit                               0x000000010944d4a3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 70318  QuartzCore                          0x000000010911759a -[CALayer layoutSublayers] + 14619  QuartzCore                          0x000000010910be70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 36620  QuartzCore                          0x000000010910bcee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 2421  QuartzCore                          0x0000000109100475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 27722  QuartzCore                          0x000000010912dc0a _ZN2CA11Transaction6commitEv + 48623  UIKit                               0x0000000109390f7c _UIApplicationHandleEventQueue + 732924  CoreFoundation                      0x000000010bceda31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 1725  CoreFoundation                      0x000000010bce395c __CFRunLoopDoSources0 + 55626  CoreFoundation                      0x000000010bce2e13 __CFRunLoopRun + 86727  CoreFoundation                      0x000000010bce2828 CFRunLoopRunSpecific + 48828  GraphicsServices                    0x000000010e051ad2 GSEventRunModal + 16129  UIKit                               0x0000000109396610 UIApplicationMain + 17130  YXB                                 0x0000000107d85cbf main + 11131  libdyld.dylib                       0x000000010c7a092d start + 1)libc++abi.dylib: terminating with uncaught exception of type NSException


出现Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer bounds contains NaN: [nan 0; nan 15]'这个错误通常有以下两种原因:

1、在内部程序中使用了sizeWithFont方法动态计算文本所需要的宽、高,可此时的文本为空,计算出来的宽或高为0;

2、在程序中在设置控件大小或位置时是先通过取到图片的大小来根据图片大小来进行设置控件的大小、位置的,此时如果当图片如果图片没有取到图片的大小将会为0,此时如果又用其它数来除了图片的大小时将会出现此类错误;


解决方案:

1、遇到此类错误需要去检查自己是否使用了sizeWithFont这个方法,如果使用了这个方法需要去做一个判断是否是因为取到的内容为空此时计算而造成计算值为0,这样就能解决此类为题

2、如果没有使用sizeWithFont方法且出现此类错误,但使用了动态获取图片大小可去判断是否是因为某个图片没有取到而造成取到的图片大小为0造成的错误


总结:

造成此类错误的最终原因主要还是因为在某个地方除了0,这在数学中也是不符合数学的计算规则的


如有不明白的地方欢迎咨询:

QQ:294491256

Telephone:13390517636




0 0
原创粉丝点击