创建UIImageView时无效的上下文

时间:2012-11-01 19:10:29

标签: ios uiimageview uiimage cgcontext

我尝试创建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上出现此错误,而不是模拟器,我不明白..:/

4 个答案:

答案 0 :(得分:40)

我遇到了这个问题,直到我发现我对resizableImageWithCapInsets的上限插入错误 - 它根本没有留下任何无上限区域(你需要至少1x1像素没有盖帽)。所以请确保:

(insets.left + insets.right)&lt;宽度

(insets.top + insets.bottom)&lt;高度

答案 1 :(得分:0)

模拟器不区分大小写。该设备区分大小写。 png名称是否正确?也许它是“登录”的大写字母。

答案 2 :(得分:0)

答案 3 :(得分:0)

由于低代表,我仍然无法发表评论。不过我想补充一下neon1的答案,这对我帮助很大。

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

insets.left&gt; 0

insets.top&gt; 0

insets.right&gt; 0

insets.bottom&gt; 0