1

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

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

这是一个 Cocoa 错误还是我做错了什么(如果这是我正在做的事情,如何纠正它)?

4

1 回答 1

1

可能是因为你设置错了edgeInsets。请确保 your edgeInsets.left + edgeInsets.rightis 小于 yourimage.size.widthedgeInsets.top + edgeInsets.bottom小于image.size.height.

或者您可能想查看此问题和答案

于 2013-08-14T05:19:05.490 回答