【问题标题】:Invalid context when creating UIImageView创建 UIImageView 时上下文无效
【发布时间】:2012-11-01 19:10:29
【问题描述】:

我在尝试创建 UIImageView 时出错。 看这段代码:

UIImage* backgroundPanel = [[UIImage imageNamed:@"loginPanelBackground.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(90, 0, 149, 416)];

self.connexionBackgroundImgView = [[UIImageView alloc] initWithImage:backgroundPanel];
self.connexionBackgroundImgView.frame = CGRectMake(0, 0, 416, 390); // THIS LINE PROVOC THE INVALID CONTEXT
[self.connexionView insertSubview:self.connexionBackgroundImgView aboveSubview:self.connexionToCreationCompteView];

它在日志中抛出这个错误:

<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextSetBlendMode: invalid context 0x0
<Error>: CGContextSetAlpha: invalid context 0x0
<Error>: CGContextTranslateCTM: invalid context 0x0
<Error>: CGContextScaleCTM: invalid context 0x0
<Error>: CGContextGetCTM: invalid context 0x0
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextClipToRect: invalid context 0x0
<Error>: CGContextDrawTiledImage: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
<Error>: CGContextGetCTM: invalid context 0x0
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextClipToRect: invalid context 0x0
<Error>: CGContextDrawTiledImage: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
<Error>: CGContextGetCTM: invalid context 0x0
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextClipToRect: invalid context 0x0
<Error>: CGContextDrawTiledImage: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0

我只在 iPad 上遇到过这个错误,在模拟器上没有,我不明白..:/

【问题讨论】:

  • 您是否使用任何自定义绘图代码?喜欢使用 CGContext 等等?
  • 也尝试改变 self.connexionBackgroundImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 416, 390)]; [self.connexionBackgroundImgView setImage:backgroundPanel];其实应该不会有太大区别。不过你还是可以试试的。
  • 您是否使用任何自定义绘图?在某处实现 CGContext?
  • 感谢 eliajf,您能否发表您的评论作为答案。那我就认了吧!

标签: ios uiimageview uiimage cgcontext


【解决方案1】:

我遇到了这个问题,直到我发现我的 resizableImageWithCapInsets: 的 cap inset 参数是错误的——它根本没有留下任何未加盖的区域(你需要至少 1x1 像素没有被盖子覆盖)。所以请确保:

(insets.left + insets.right)

(insets.top + insets.bottom)

【讨论】:

  • 这正是我的情况!我正在使用 resizableImageWithCapInsets 并且 frame 小于 cap insets,所以它会抛出这个警告......
  • 很好的提示,我搜索了很长时间才找到这个:-)
【解决方案2】:

模拟器不区分大小写。该设备区分大小写。 png的命名是否正确?可能是用大写的 L 进行“登录”。

【讨论】:

  • 图像在模拟器和 iPad 上都能正确显示。日志没有任何变化,但我想知道为什么会出现此错误:/
【解决方案3】:
【解决方案4】:

由于代表人数少,我仍然无法发表评论。不过,我想补充一下对我有很大帮助的 neon1 的回答。

您还需要确保所有插图都大于零。

insets.left > 0

insets.top > 0

insets.right > 0

insets.bottom > 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多