Drawing images on top of each other
Drawing images on top of each other
- Subject: Drawing images on top of each other
- From: Andrew Coad <email@hidden>
- Date: Fri, 4 Feb 2011 01:58:06 -0500
- Importance: Normal
I'm stuck on something that should be simple but it is evading me. I have searched this mailing list and regular documentation but can't figure out an answer.
I'm using the iOS platform. Previously, my drawing code needed to draw images at different locations in the view erasing everything prior to that (i.e. there was only one image visible at any one time). To do this I simply invalidated the entire view with [theView setNeedsDisplay] and in drawRect:aRect I drew the new image. This all works fine. However, now, I want multiple images visible at the same time and selectively replace images as necessary (by 'overwriting' them). My approach has been to invalidate just the region within the view that I want to draw into using [theView setNeedsDisplayInRect:aRect] and then draw the new image at the desired location (i.e. inside aRect). This doesn't work hence this email. The problem is that the old image is not erased prior to drawing the new image hence all the images 'accumulate'. Here is the relevant code:
View creation:
[self setTouchSequenceViewSys0:[[RTTouchSequenceView alloc] initWithFrame:baseRect]];
[[self touchSequenceViewSys0] setAlpha:1.0];
[[self touchSequenceViewSys0] setOpaque:NO];
[[self touchSequenceViewSys0] setClearsContextBeforeDrawing:YES];
[[self touchSequenceViewSys0] setTouchClearImage:[[self imageArrayLookup] objectForKey:@"touchClear"]];
[[self touchSequenceViewSys0] setTouchSequence:nil]; // to prevent any drawing when view is added as a subview
[[self view] addSubview:[self touchSequenceViewSys0]];
View invalidation:
CGRect aRect = CGRectMake([[[[self touchSequence] objectAtIndex:[self touchDisplayStartIndex]]
beginDrawLocationX] intValue],
[[[[self touchSequence] objectAtIndex:[self touchDisplayStartIndex]]
beginDrawLocationY] intValue],
[[[[self touchSequence] objectAtIndex:tmpIndex]
beginDrawLocationX] intValue]
- [[[[self touchSequence] objectAtIndex:touchDisplayStartIndex]
beginDrawLocationX] intValue]
+ [[[[self touchSequence] objectAtIndex:tmpIndex]
displayImage] size].width,
[[[[self touchSequence] objectAtIndex:tmpIndex]
beginDrawLocationY] intValue]
- [[[[self touchSequence] objectAtIndex:touchDisplayStartIndex]
beginDrawLocationY] intValue]
+ [[[[self touchSequence] objectAtIndex:tmpIndex]
displayImage] size].height);
[[self touchSequenceViewSys0] setTouchSequence:[self touchSequence]];
[[self touchSequenceViewSys0] setTouchDisplayStartIndex:[self touchDisplayStartIndex]];
[[self touchSequenceViewSys0] setTouchDisplayEndIndex:tmpIndex];
[[self touchSequenceViewSys0] setNeedsDisplayInRect:aRect];
View drawing:
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
if (touchSequence) {
BOOL isDisplayDone = NO;
while (!isDisplayDone) {
RTTouchSequenceItem *item = [[self touchSequence] objectAtIndex:[self touchDisplayStartIndex]];
CGPoint aPoint = CGPointMake([[item beginDrawLocationX] doubleValue],
[[item beginDrawLocationY] doubleValue]);
//[[self touchClearImage] drawAtPoint:aPoint blendMode:kCGBlendModeNormal alpha:0.0]; // <-- see note below
[[item displayImage] drawAtPoint:aPoint
blendMode:kCGBlendModeNormal alpha:[[item displayImageAlpha] doubleValue]]; // <-- alpha is in fact 1.0
if ([self touchDisplayStartIndex] == [self touchDisplayEndIndex]) {
isDisplayDone = YES;
}
else {
[self setTouchDisplayStartIndex:[self touchDisplayStartIndex] + 1];
}
}
}
[self setTouchSequence:nil]; // can do this since touchSequence is not retained
}
The commented out line:
[[self touchClearImage] drawAtPoint:aPoint blendMode:kCGBlendModeNormal alpha:0.0];
was an attempt to draw a transparent black rectangle first followed by the desired image. That had no effect. I've also tried variations on kCGBlendMode, alpha and opaque settings but, under all combinations, fundamentally, the new image is additive with respect to the old image(s).
How do I clear a region on the view prior to drawing a new image? Apologies is this is a hopelessly 'newbie' question.....
Andrew Coad
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden