Ben Flynn
Ben Flynn

Reputation: 18922

UIButton setImage vs setBackgroundImage with resizable images

I'm trying to use a resizable image as a button background.

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

When I run this code, my button background is set properly, but I get all kinds of log errors:

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

Is this a Cocoa bug or am I doing something wrong (and how to correct it if it's something I'm doing)?

Upvotes: 1

Views: 1002

Answers (1)

liuyaodong
liuyaodong

Reputation: 2567

Probably because your have set the wrong edgeInsets. Please make sure that your edgeInsets.left + edgeInsets.right is less than your image.size.width and edgeInsets.top + edgeInsets.bottom less than image.size.height.

Or you probably want to review this question&answer

Upvotes: 1

Related Questions