gpt4 book ai didi

ios - 什么可能导致此 "Fatal Exception: CALayerInvalidGeometry CALayer bounds contains NaN: [nan nan; nan nan]"崩溃?

转载 作者:行者123 更新时间:2023-11-29 02:17:57 32 4
gpt4 key购买 nike

我有以下代码,它简单地初始化一个 UIImageView 以适应 UIImage 在当前屏幕尺寸上尽可能大的比例:

CGSize mainScreenSize = [appDelegate mainScreenSize];
CGSize superViewSize = CGSizeMake(mainScreenSize.width, mainScreenSize.height);
CGSize size = [self.image size];

CGFloat xScale = superViewSize.width / size.width;
CGFloat yScale = superViewSize.height / size.height;

CGFloat minimumScale = MIN(xScale, yScale);
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.width * minimumScale, size.height * minimumScale)];

self.imageView.image = self.image;
self.frame = CGRectMake(0, 0, size.width * minimumScale, size.height * minimumScale);

[self addSubview:self.imageView];

出于某种原因,当我的一些用户正在使用该应用程序时,设置 UIImageViewframe 的代码部分导致以下内容崩溃:

Fatal Exception: CALayerInvalidGeometry CALayer bounds contains NaN: [nan nan; nan nan]

在检查我的崩溃日志时,错误似乎在所有 iOS 版本、设备和方向上都是一致的,所以我已经排除了这种可能性。作为旁注,我用来实现上面的代码是这样的:

self.imageView = [[UIImageView alloc] initWithImage:image];
self.frame = CGRectMake(0, 0, self.imageSize.width, self.imageSize.height);
[self addSubview:self.imageView];

然后在它附加到 superview 之后,我将对 UIImageView 执行转换以调整它的大小并且我从未收到上述错误(由于解决由另一个框架引起的错误,我不得不更改为新方法)。

我知道上述错误通常是由除以 0 或将框架宽度和高度设置为 0 引起的,但我很难弄清楚这是怎么发生的。我不认为问题出在 imageSize 上,因为正如我之前所说,我以前的方法从来没有遇到过这个问题。至于主屏幕大小,这是我的应用程序委托(delegate)中的方法:

- (CGSize) mainScreenSize
{
CGSize mainScreenSize = [[UIScreen mainScreen] bounds].size;

if ([OperatingSystemVersion operatingSystemVersionLessThan:@"8.0"] && UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
{
mainScreenSize = CGSizeMake(mainScreenSize.height, mainScreenSize.width);
}

return mainScreenSize;
}

有人知道是什么原因造成的吗?

实际上意识到我在 .h 中像这样设置我的图像属性:

@interface DynamicImageView : UIView <UIGestureRecognizerDelegate>
{
UIImage *_image;
}
@property(nonatomic, strong) UIImage *image;

但我并没有在我的 .m 文件中合成它。这会导致问题吗?我想知道这在大多数情况下是如何工作的,只是有时会崩溃?

最佳答案

原因是当用户从照片库加载照片时,如果没有足够的内存来加载照片,则 UIImage< 将会返回 nil/。所以我上面的代码试图根据 nil UIImage 的大小创建一个带有 frameUIImageView 。导致其崩溃。我通过处理从 UIImagePickerController 返回 nil UIImage 的情况来修复此问题。

关于ios - 什么可能导致此 "Fatal Exception: CALayerInvalidGeometry CALayer bounds contains NaN: [nan nan; nan nan]"崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28532276/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com