I have a working program now doing what I want. I made it by subclassing UIView, adding a UIImageView subview containing the bottom image I want to use and then added the image I wanted on top of it in the drawRect method:
Code:
-( id )initWithItemUnderlay:( UIImage * )itemUnderlay frame:( CGRect )frame
{
if( self = [super initWithFrame:frame] )
{
UIImageView *imageView = [[UIImageView alloc] initWithImage: itemUnderlay];
[self addSubview:imageView];
[imageView release];
overlayImage = NO;
}
return self;
}
-( void )drawRect:( CGRect )rect
{
if( overlayImage )
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage( context, rect, overlayImage );
}
}
I call it with this in my main class:
Code:
itemView = [[ItemView alloc] initWithItemUnderlay:defaultItemUnderlay
frame:CGRectMake( ITEM_VIEW_X, ITEM_VIEW_Y, ITEM_VIEW_WIDTH, ITEM_VIEW_HEIGHT )];
[itemView setOverlayImage:defaultOverlayImage];
[customTableView addSubview:itemView];
This gives me the default images. However, if I try and change the top image (overlayImage) using this code:
Code:
[itemView setOverlayImage:[item image]];
[itemView drawRect:CGRectMake( ITEM_VIEW_X, ITEM_VIEW_Y, ITEM_VIEW_WIDTH, ITEM_VIEW_HEIGHT )];
It doesn't change the UIView at all... I also get this error message in the console:
Code:
<Error>: CGContextDrawImage: invalid context
If anyone knows how I can re-draw my overlayImage after I have set the default underlay/overlay images I would appreciate the help...