UIButton setImage vs setBackgroundImage具有可调整大小的图像

时间:2013-08-14 00:10:07

标签: ios objective-c cocoa uibutton core-graphics

我正在尝试使用可调整大小的图片作为按钮背景。

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

这是一个可可虫还是我做错了(如果我正在做的话,如何纠正它?)

1 个答案:

答案 0 :(得分:1)

可能是因为您设置了错误的edgeInsets。请确保您的edgeInsets.left + edgeInsets.right小于image.size.widthedgeInsets.top + edgeInsets.bottom小于image.size.height

或者您可能想要查看this question&answer