我正在尝试使用可调整大小的图像作为按钮背景。
UIImage *image = [UIImage imageNamed:@"btn_prev_secondary"];
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(30.0f, 25.0f, 29.0f, 9.0f);
UIImage *resizeableImage = [image resizableImageWithCapInsets:edgeInsets];
// What I'd like to do:
[self.backButton setBackgroundImage:resizeableImage forState:UIControlStateNormal];
// This works (but doesn't do what I want)
// [self.backButton setImage:resizeableImage forState:UIControlStateNormal];
// Also works (also doesn't do what I want)
// [self.backButton setImage:image forState:UIControlStateNormal];
当我运行此代码时,我的按钮背景设置正确,但是出现各种日志错误:
Aug 13 17:05:25 MyApp <Error>: CGContextSaveGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextSetBlendMode: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextSetAlpha: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextTranslateCTM: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextScaleCTM: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextGetCTM: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextSaveGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextClipToRect: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextDrawTiledImage: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextRestoreGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextGetCTM: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextSaveGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextClipToRect: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextDrawTiledImage: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextRestoreGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextGetCTM: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextSaveGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextClipToRect: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextDrawTiledImage: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextRestoreGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextGetCTM: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextSaveGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextClipToRect: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextDrawTiledImage: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextRestoreGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextGetCTM: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextSaveGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextClipToRect: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextDrawTiledImage: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextRestoreGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextGetCTM: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextSaveGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextClipToRect: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextDrawTiledImage: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextRestoreGState: invalid context 0x0
Aug 13 17:05:25 MyApp <Error>: CGContextRestoreGState: invalid context 0x0
这是可可错误还是我做错了什么(如果我正在做的话如何纠正它)?
最佳答案
可能是因为您设置了错误的edgeInsets
。请确保您的edgeInsets.left + edgeInsets.right
小于image.size.width
,并且edgeInsets.top + edgeInsets.bottom
小于image.size.height
。
或者您可能想查看this question&answer
关于ios - UIButton setImage与setBackgroundImage和可调整大小的图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18221247/