Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

Thread: Dynamic Mask
View Single Post
Old 07-13-2011, 02:21 AM   #16 (permalink)
new2objectivec
Registered Member
 
Join Date: Jun 2011
Location: Sydney, Australia
Posts: 44
new2objectivec is on a distinguished road
Default

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;
}

Last edited by new2objectivec; 07-13-2011 at 05:47 AM.
new2objectivec is offline   Reply With Quote
 

» Advertisements
» Online Users: 404
16 members and 388 guests
Absentia, alisafl22, bbbsaha, DaveDee, dbramhall, erdinc27, fredidf, Hercule, heshiming, Johnsyfared, milanalina, prbiztech, roof44, Tusarari, waghman, wserver
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,603
Threads: 94,084
Posts: 402,783
Top Poster: BrianSlick (7,990)
Welcome to our newest member, alisafl22
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:02 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.