View Full Version : Subview UIImage problems

Jun 18, 2008, 09:56 AM
I've created an object called "PictureView", it contains a UIImage called pictureImg (for the image I want to display) and creates a frame based on the size of the image, at my chosen X,Y.

I've created 4 of these and added them as subviews to my view.

I have been trying to change the image they display, to no avail.

Should I be able to change the image in my instance, and watch my subview update, or do I need to re-add the subview once the image has been changed?

If I try to change images, I get several error messages in the console;

<Error>:CGContextSaveGState: invalid context
<Error>:CGContextSetBlendMode: invalid context
<Error>:CGContextSetAlpha: invalid context
<Error>:CGContextTranslateCTM: invalid context
<Error>:CGContextDrawImage: invalid context
<Error>:CGContextRestoreGState: invalid context

Any ideas what this means?

Jun 18, 2008, 01:41 PM
Can you post some specific code? You should be able to do what you describe. Maybe we can spot a bug with what you're doing.

Jun 24, 2008, 09:24 AM
Sorry this has taken so long to get back around.

I first create a view controller etc. Then I create a new instance of PictureView. I store the name of 2 images in newPicture when not pressed, I would like "pic1.png" to be displayed. When pressed, "pic2.png" should be displayed.

App delegate:

PictureView *newPicture = [[PictureView alloc] init];

[newPicture setImage1: @"pic1.png"];
[newPicture setImage2: @"pic2.png"];

[myView addSubview: [newPicture updatePicture]];


- (id) init {
UIImage *image = [UIImage imageNamed:@"null_pic.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);

if (self = [self initWithFrame:frame]) {

self.opaque = NO;
pictureImage = image;

- (void)drawRect:(CGRect)rect {

[self reDrawPic];

- (void)reDrawPic {

[pictureImage drawAtPoint:(CGPointMake(0.0, 0.0))];

- (void)updatePicture {

UIImage *image = [UIImage imageNamed: [self getImgFileName]];

pictureImage = image;

[self reDrawPic];

The getImgFileName function simply returns the name of the image file I want to display, that works fine.

So, the init function creates a frame the same size as my temporary png file (which is the same size as my pic1 and pic2 files), I then call the updatePicture function when the picture is added as a subview and *that works* - my pic1 image is displayed.

I call updatePicture when I want to change the on-screen image. The UIImage *image is always the correct image file, but nothing visually changes.

The only difference between calling updatePicture now and when adding the subview, is the errors detailed in my first post.

Any help would be appreciated - I have not figured this out in a week so far..