I am still new to drawing but based on the code from the following 2 links:
http://www.iphonedevsdk.com/forum/245649-post15.html
[Tutorial] Drawing to the screen. - iFans - iPad, iPhone, and iPod touch Fans forums
I created a test project which might be able to achieve something "similar" to what the original question is asking.
I have one bottom imageview to show an image, then add on top another view to allow user to draw with finger.
What I achieved so far is: user can draw something with finger on top, then click on "switch" button to switch to "erase" mode to erase the colour and it will reveal the image at the bottom.
Problem is I don't know how to fill the whole view screen with a specific colour yet. If this can be done on the upper layer before loading it, then remove the "switch" button and go straight to "erase" mode, the hidden image at the bottom will be revealed on the user's finger movement.
It shouldn't be too difficult to fill up the whole screen with a specific colour, right? Any one, please?
Code below, please let me know if any problem. Thanks!
[UPDATED] Also posted on my blog:
http://new2objectivec.blogspot.com/2...t-project.html
Note: please replace the "watermelon.jpg" image name in the following code with any other image file you have. OR visit my blog for the full project code including the image.
Code:
// based on code from http://www.iphonedevsdk.com/forum/245649-post15.html
// and http://www.ifans.com/forums/showthread.php?t=132024
#import <UIKit/UIKit.h>
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
@interface TestBedViewController : UIViewController {
CGPoint lastPoint;
UIImageView *drawImage;
BOOL mouseSwiped;
int mouseMoved;
BOOL drawMode;
}
@end
@implementation TestBedViewController
BOOL mouseSwiped=NO;
-(void) createNormalButton: (UIButton *) buttonObj
atPosX: (double) buttonPositionX
atPosY: (double) buttonPositionY
withWidth: (double) buttonWidth
withHeight: (double) buttonHeight
withBGColor: (UIColor *) buttonBGColor
withTitleColor: (UIColor *) buttonTitleColor
withTag: (int) buttonTag
withTitle: (NSString *) buttonTitle
withFontSize: (int)buttonFontSize
withSelfID: (id)buttonSelfID
withActionID: (SEL)selectorID
ifEnabled: (BOOL)buttonEnabled
inView: (UIView *)viewToAddTo
{
buttonObj = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonObj setFrame:CGRectMake(buttonPositionX, buttonPositionY, buttonWidth, buttonHeight)];
[buttonObj setTitle: buttonTitle forState:UIControlStateNormal];
[buttonObj.titleLabel setFont:[UIFont systemFontOfSize:buttonFontSize]];
[buttonObj setTag: buttonTag];
[buttonObj addTarget:buttonSelfID action:selectorID forControlEvents:UIControlEventTouchUpInside];
if (buttonEnabled) {
[buttonObj setEnabled:YES];
} else {
[buttonObj setEnabled:NO];
}
[viewToAddTo addSubview:buttonObj];
[[buttonObj retain]autorelease];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
drawImage.image = nil;
return;
}
lastPoint = [touch locationInView:self.view];
lastPoint.y -= 20;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y -= 20;
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
if (drawMode) {
CGContextSetBlendMode(UIGraphicsGetCurrentContext( ),kCGBlendModeNormal);
} else {
CGContextSetBlendMode(UIGraphicsGetCurrentContext( ),kCGBlendModeClear);
}
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext( ), 25.0);
if (drawMode) {
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
} else {
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor clearColor] CGColor]);
}
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext()) ;
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
drawImage.image = nil;
return;
}
if(!mouseSwiped) {
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 20.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 1.0, 1.0, 0.5);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
- (void)loadView {
self.view=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor=[UIColor grayColor];
UIImage *image=[UIImage imageNamed:@"watermelon.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,300,300)];
imageView.image = image;
[self.view addSubview:imageView];
[super viewDidLoad];
drawImage = [[UIImageView alloc] initWithImage:nil];
drawImage.frame = self.view.frame;
[self.view addSubview:drawImage];
mouseMoved = 0;
drawMode=YES;
UIButton *buttonBeginning;
[self createNormalButton: buttonBeginning
atPosX: 0
atPosY: 0
withWidth: 50
withHeight: 30
withBGColor: [UIColor whiteColor]
withTitleColor: [UIColor blackColor]
withTag: 12345
withTitle: @"switch"
withFontSize: 15
withSelfID: self
withActionID: @selector(buttonPressed:)
ifEnabled: YES
inView: self.view];
}
- (IBAction) buttonPressed: (id) sender
{
drawMode = ! drawMode;
}
- (void)dealloc {
[super dealloc];
}
@end
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
@interface TestBedAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
TestBedViewController *viewController;
}
@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) TestBedViewController *viewController;
@end
@implementation TestBedAppDelegate
@synthesize window;
@synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[application setStatusBarHidden:YES];
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [TestBedViewController alloc];
[self.window addSubview:self.viewController.view];
[self.viewController release];
[self.window makeKeyAndVisible];
}
- (void)dealloc {
[window release];
[super dealloc];
}
@end
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"TestBedAppDelegate");
[pool release];
return retVal;
}