tags:

views:

72

answers:

1

Hey Guys,

I am creating custom cell in my iphone aap and I am also adding an image to it but I continue to get error at [image drawatpoint:p];

the error states *** -[UIImage drawAtPoint:]: message sent to deallocated instance 0x45cb1f0

my code is

-(void)setImage:(UIImage *)i
{
    //[image release];
    image = i;
    [self setNeedsDisplay]; 
}

- (void)setNeedsDisplay
{
    [super setNeedsDisplay];
    [contentView setNeedsDisplay];
}

- (void)drawContentView:(CGRect)r
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    UIColor *backgroundColor = [UIColor yellowColor];
    UIColor *textColor = [UIColor blackColor];

    if(self.selected)
    {
     backgroundColor = [UIColor clearColor];
     textColor = [UIColor whiteColor];
    }

    [backgroundColor set];
    CGContextFillRect(context, r);

    CGPoint p;
    p.x = 3;
    p.y = 3;

    [textColor set];
    UIImage *image2 = image;
    [image2 drawAtPoint:p];

    p.x += 70; // space between words
    [text2 drawAtPoint:p withFont:firstTextFont];

    //[image release];
}
+1  A: 

You do not retain the image in your setter (setImage:). As the error message says, it's gone at the time you try to draw it.

Nikolai Ruhe
I added image = [i retain];That fixed the problem
harekam_taj
Now that you have added [i retain];, you must also uncomment the [image release]; call in setImage: to avoid memory leaks.
Ole Begemann
Ok sounds great
harekam_taj