Invalid context when creating UIImageView

蹲街弑〆低调 提交于 2019-12-04 09:19:23

问题


I have an error when I tried to create a UIImageView. Look at this code :

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];

It throws this error in the log :

<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

I had this error only on the iPad, not with the Simulator, I don't understand.. :/


回答1:


I had this problem until I found out that my cap inset argument to resizableImageWithCapInsets: was wrong — it didn't leave any un-capped area at all (you need at least 1x1 pixel not covered by a cap). So make sure that:

(insets.left + insets.right) < width

and

(insets.top + insets.bottom) < height




回答2:


The Simulator is case-insensitive. The device is case-sensitive. The png is named correctly? Maybe it is 'Login' with a capital L.




回答3:


See the answer here : How do I capture UIImage of complete contents of UITableView / UIScrollView and make it work on an ios device

Thanks eliajf !




回答4:


I still can't comment due to low rep. However I'd like to add to neon1's answer which helped me a lot.

You also need to make sure that all of the insets are greater than zero.

insets.left > 0

insets.top > 0

insets.right > 0

insets.bottom > 0



来源:https://stackoverflow.com/questions/13183904/invalid-context-when-creating-uiimageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!