03-29-2010, 08:50 AM
#1 (permalink )
I will be a billionaire.
Join Date: Sep 2009
Location: Scarborough, United Kingdom
Age: 17
Posts: 1,344
Paint App
I have this code which creates a simple paint app
Code:
- (void)viewDidLoad {
[super viewDidLoad];
drawImage = [[UIImageView alloc] initWithImage:nil];
drawImage.frame = self.view.frame;
[self.view addSubview:drawImage];
self.view.backgroundColor = [UIColor whiteColor];
mouseMoved = 0;
}
- (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)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 10) {
mouseMoved = 0;
}
}
- (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(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
It works perfectly however how can I change the colour of the paint from a different view?
Thanks
03-29-2010, 09:08 AM
#2 (permalink )
Daddy Cool
iPhone Dev SDK Supporter
Join Date: Mar 2009
Location: Yorkshire, England
Age: 100
Posts: 1,616
If you're thinking about passing variables between views, look no further than the example in this link:
Singleton Classes Matt Galloway's iPhone Apps
Bloody great it is - much better than global delegate variables!
03-29-2010, 09:11 AM
#3 (permalink )
I will be a billionaire.
Join Date: Sep 2009
Location: Scarborough, United Kingdom
Age: 17
Posts: 1,344
Cool link, thanks.
How could I do it from the same view?
Thanks
03-29-2010, 12:20 PM
#4 (permalink )
Registered Member
Join Date: Mar 2010
Posts: 173
Not sure what u mean, but if u want to change the color of the pen, then
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentCon text(), 1.0, 0.0, 0.0, 1.0);
1.0, 0.0, 0.0, 1.0 = Red, Green, Blue, Alpha.
Now it is painting red yeah? So u just need to give user option to select a color from a pallete image, (or easier way use 3 sliders with min val of 0 and max of 1). then put the variables in that function.
03-29-2010, 12:41 PM
#5 (permalink )
I will be a billionaire.
Join Date: Sep 2009
Location: Scarborough, United Kingdom
Age: 17
Posts: 1,344
sorry to be a pain but do you have any code for that?
I'm currently on my phone so can't search for anything!
04-09-2010, 06:48 AM
#6 (permalink )
I will be a billionaire.
Join Date: Sep 2009
Location: Scarborough, United Kingdom
Age: 17
Posts: 1,344
I couldn't find anything on this. Can anyone help me out?
05-08-2010, 10:18 AM
#7 (permalink )
Registered Member
Join Date: Mar 2010
Posts: 5
Particle image as pen
Hi Guys,
Any tips for particles as image, I want to use the image to draw and drag. Any tips?
thanks,
-Jd
09-01-2010, 10:59 PM
#8 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 138
Quote:
Originally Posted by
IphoneSdk
I have this code which creates a simple paint app
Code:
- (void)viewDidLoad {
[super viewDidLoad];
drawImage = [[UIImageView alloc] initWithImage:nil];
drawImage.frame = self.view.frame;
[self.view addSubview:drawImage];
self.view.backgroundColor = [UIColor whiteColor];
mouseMoved = 0;
}
- (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)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 10) {
mouseMoved = 0;
}
}
- (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(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
It works perfectly however how can I change the colour of the paint from a different view?
Thanks
Code works like a charm. Just one favor. Do you have a clear screen button on your app. If so can you share the code ? Apple has a paint program that is so complex it is hard to follow. Look for MSPaint. It has a paint selector.
09-01-2010, 11:10 PM
#9 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 138
I know begers can't be choosers but I was wondering if you cant limit the paint area to a certain percentage of the screen.
09-04-2010, 02:28 PM
#10 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 138
any help.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 349
13 members and 336 guests
akacaj , c2matrix , cgokey , esoteric , GHuebner , givensur , HemiMG , Mirotion22 , mjnafjke , Pudding , SLIC , Sonuye857 , Techgirl-52
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,652
Threads: 94,115
Posts: 402,887
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Sonuye857