How to erase finger paint on Custom UIView in iPhone - iphone

I have created a custom UIView (without .xib) for a finger paint application.
Paint is working fine with custom UIView but my problem is that when I try to erase the painted path I am getting:
Error : Invalid context
Below is my class:
.h file
#interface draw2D : UIView
{
CGPoint previousPoint;
CGPoint lastPoint;
CGMutablePathRef path;
UIButton *btnClose;
UIButton *btnErase;
BOOL IsErase;
}
- (IBAction)btnClose:(id)sender;
- (IBAction)btnErase:(id)sender;
#end
#implementation draw2D
- (void)awakeFromNib
{
path = CGPathCreateMutable();
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
btnClose = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnClose addTarget:self action:#selector(btnClose:)
forControlEvents:UIControlEventTouchDown];
[btnClose setTitle:#"close" forState:UIControlStateNormal];
btnClose.frame = CGRectMake(10, 10, 100, 40.0);
btnErase = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnErase addTarget:self action:#selector(btnErase:)
forControlEvents:UIControlEventTouchDown];
[btnErase setTitle:#"Erase" forState:UIControlStateNormal];
btnErase.frame = CGRectMake(150, 10, 100, 40.0);
[self addSubview:btnClose];
[self addSubview:btnErase];
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
NSLog(#"Touch Began :%d",[touch tapCount]);
if ([touch tapCount] > 1)
{
NSLog(#"::::: Paint Start :::::");
path = CGPathCreateMutable();
previousPoint = lastPoint;
[self setNeedsDisplay];
}
self.backgroundColor = [UIColor clearColor];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"::::: touchesMoved :::::");
lastPoint = [[touches anyObject] locationInView:self];
previousPoint = [[touches anyObject] previousLocationInView:self];
if(IsErase)
{
NSLog(#"erase");
UITouch *erasetouch = [touches anyObject];
CGPoint erasecurrentPoint = [erasetouch locationInView:self];
CGContextRef erasecontext = UIGraphicsGetCurrentContext();
CGContextSetLineCap(erasecontext, kCGLineCapRound);
CGContextSetLineWidth(erasecontext,10);
CGContextSetBlendMode(erasecontext, kCGBlendModeClear);
CGContextSetStrokeColorWithColor(erasecontext, [[UIColor clearColor] CGColor]);
CGContextBeginPath(erasecontext);
CGContextMoveToPoint(erasecontext, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(erasecontext, erasecurrentPoint.x, erasecurrentPoint.y);
CGContextStrokePath(erasecontext);
CGContextFlush(erasecontext);
}
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
NSLog(#"::::: drawRect :::::");
CGContextRef context = UIGraphicsGetCurrentContext();
CGPathMoveToPoint(path, NULL, previousPoint.x, previousPoint.y);
CGPathAddLineToPoint(path, NULL, lastPoint.x, lastPoint.y);
CGContextAddPath(context, path);
CGContextSetLineWidth(context, 5);
[[UIColor blueColor] setStroke];
CGContextDrawPath(context, kCGPathFillStroke);
}
- (IBAction)btnClose:(id)sender
{
[self removeFromSuperview];
}
- (IBAction)btnErase:(id)sender
{
IsErase = YES;
}
#end
I have set erase button with functionality but not working.

The problem you have is that you are not supposed to call UIGraphicGetContext() outside of drawRect:
In your touchesBegan:withEvent: and touchesMoved:withEvent: you should simply store the points you want to draw and call [self setNeedsDisplay] as you are doing now.
In your drawRect: implementation you would then draw the points you have stored.
You can take a look at this github repo that provides an implementation for a smooth drawing:https://github.com/levinunnink/Smooth-Line-View

finally i have find the solution . my mistake was i m implement erase code intouchMove method with new context . i dont have to need new context . implement erase code in drawrect method and now its working fine . see the below code.
- (void)drawRect:(CGRect)rect
{
[curImage drawAtPoint:CGPointMake(0, 0)];
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
context = UIGraphicsGetCurrentContext();
[self.layer renderInContext:context];
if(IsErase)
{
CGContextSetLineWidth(context,self.lineWidth);
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextSetStrokeColorWithColor(context, [[UIColor clearColor] CGColor]);
CGContextBeginPath(context);
CGContextMoveToPoint(context, mid1.x, mid1.y);
CGContextAddLineToPoint(context, previousPoint1.x, previousPoint1.y);
CGContextStrokePath(context);
CGContextFlush(context);
}
else
{
CGContextMoveToPoint(context, mid1.x, mid1.y);
CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextSaveGState(context);
CGContextStrokePath(context);
}
[super drawRect:rect];
[curImage release];
}
i hope it will helps to someone in erase functionality .

Related

Clear a drawing in drawRect

How can I clear a drawing in a subview? What should I put in my (void)clearLine below?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Setup subview to draw a line
CGRect subviewRect = CGRectMake(0, 0, 250, 300);
Draw* _draw = [[Draw alloc] initWithFrame:subviewRect];
_draw.exclusiveTouch = NO;
_draw.backgroundColor = [UIColor clearColor];
[self addSubview:_draw];
}
return self;
}
- (void) clearLine
{
// how can I clear the drawing in Draw
}
And below is the Draw.m file, that will draw a straight line by getting the first touch as the starting point and the second touch as the ending point.
#implementation Draw
- (void)drawRect:(CGRect)rect {
CGPoint fromPoint;
CGPoint toPoint;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
}
// Touch event
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touchPoint = [touches anyObject];
fromPoint = [touchPoint locationInView:self];
toPoint = [touchPoint locationInView:self];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
toPoint = [touch locationInView:self];
[self setNeedsDisplay];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
toPoint = [touch locationInView:self];
[self setNeedsDisplay];
}
In clear line you should remove Draw view and set it nil
Draw* _draw;
- (void) clearLine
{
[_draw removeFromSuperview];
_draw=nil;
}
and to again enable drawing use
-(void) initDrawView
{
CGRect subviewRect = CGRectMake(0, 0, 250, 300);
if(_draw!=nil)
{
[_draw removeFromSuperview];
_draw=nil;
}
_draw = [[Draw alloc] initWithFrame:subviewRect];
_draw.exclusiveTouch = NO;
_draw.backgroundColor = [UIColor clearColor];
[self addSubview:_draw];
}
Draw the background color on your view,
Like if you have white background then [UIColor whiteColor];
if you have black background then [UIColor blackColor];
Make a Bool variable in Draw class
And when you want to clear all like this
- (void) clearLine
{
// Here you can clearn lines
[_draw CleanAllLines]
}
And In Draw class make the method
-(void)CleanAllLines{
isNeedToClear = YES;
[self setNeedsLayout];
}
- (void)drawRect:(CGRect)rect {
if(isNeedToClear){
isNeedToClear = NO;
return;
}
CGPoint fromPoint;
CGPoint toPoint;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
}

How to unerase an erased UIImage?

I am trying an image erasing code in my PanGesture. I am using the code below:
UIImage * image = [UIImage imageNamed:#"326d4fb72362a2e44659141cadfc4977.jpg"];
holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 235)];
imgForeground = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imgForeground setImage:image];
[holderView addSubview:imgForeground];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
[self.view addSubview:holderView];
Then I am handling this here:
-(void)move:(id)sender {
CGPoint currentPoint;
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan){
lastPoint = [sender locationInView:imgForeground];
lastPoint.y -=20;
}
else{
currentPoint = [sender locationInView:imgForeground];;
currentPoint.y -=20;
}
UIGraphicsBeginImageContext(imgForeground.frame.size);
[imgForeground.image drawInRect:CGRectMake(imgForeground.frame.origin.x, imgForeground.frame.origin.y, imgForeground.frame.size.width, imgForeground.frame.size.height)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineCap(context,kCGLineCapRound); //kCGImageAlphaPremultipliedLast);
CGContextSetLineWidth(context, 10);
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
CGContextBeginPath(context);
CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(context, lastPoint.x, lastPoint.y);
CGContextStrokePath(context);
imgForeground.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
How to unerase the same image?
I have the same issue in my app after the long search i found the simple solution for this issue.
i just used touches methods of the UIViewController
The below is the my approach,
Code:-
#pragma mark touches stuff...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
lastTouch = [touch locationInView:self.editedImageView];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
currentTouch = [touch locationInView:self.editedImageView];
CGFloat brushSize;
if (isEraser)
{
brushSize=eraser;
}
else
{
brushSize=mark;
}
CGColorRef strokeColor = [UIColor whiteColor].CGColor;
UIGraphicsBeginImageContext(self.editedImageView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.editedImageView.image drawInRect:CGRectMake(0, 0, self.editedImageView.frame.size.width, self.editedImageView.frame.size.height)];
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, brushSize);
if (isEraser) {
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:self.im].CGColor);
}
else
{
CGContextSetStrokeColorWithColor(context, strokeColor);
CGContextSetBlendMode(context, kCGBlendModeClear);
}
CGContextBeginPath(context);
CGContextMoveToPoint(context, lastTouch.x, lastTouch.y);
CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
CGContextStrokePath(context);
self.editedImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastTouch = [touch locationInView:self.editedImageView];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
The Inputs:
self.editedImageView.image = "Your Custom image";
self.im = "Your Custom image";
The simple solution of your problem will be :-
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:self.im].CGColor);
Note:
This not unerase it again drawing the image over
Update:-
self.im = "Your Custom image";
this will be like this....
-(void)eraserConfigure
{
UIImageView *resizeImage=[[[UIImageView alloc]initWithImage:editedImage]autorelease];
/*UIImage */self.im=[UIImage imageFromView:resizeImage scaledToSize:CGSizeMake(self.editedImageView.frame.size.width, self.editedImageView.frame.size.height)];
}
Save a temporary image after every drawing operation.
You should save on temporary files and limit the number of undo operations.

Drawing a line between the previous end point to next start point

i'm stuck at this problem. I need to draw a line between the previous end point to next start point. My code for drawing a line between 2 points is
- (void)drawRect:(CGRect)rect
{
CGContextRef c=UIGraphicsGetCurrentContext();
CGContextSetLineWidth(c, 4.0);
CGFloat red[4]={0.0f, 0.0f, 0.0f, 1.0f};
CGContextSetStrokeColor(c, red);
CGContextBeginPath(c);
CGContextMoveToPoint(c, 50.0f, 50.0f);
CGContextAddLineToPoint(c, 100.0f, 100.0f);
CGContextStrokePath(c) ;
}
How can i give multiple points, so that i can it shows like a graph.
Thanks in advance
On a side note, Apple prefers that you use the higher UIKit methods instead of the Core Graphics calls for simple stuff like drawing lines and curves, so you could rewrite your method like this:
[[UIColor redColor] setStroke];
UIBezierPath *linePath = [UIBezierPath bezierPath];
[linePath moveToPoint:CGPointMake(50.0, 50.0)];
[linePath addLineToPoint:CGPointMake(100.0, 100.0)];
[linePath setLineWidth:4.0];
[linePath stroke];
Just add more CGContextAddLineToPoint calls
- (void)drawRect:(CGRect)rect
{
CGContextRef c=UIGraphicsGetCurrentContext();
CGContextSetLineWidth(c, 4.0);
CGFloat red[4]={0.0f, 0.0f, 0.0f, 1.0f};
CGContextSetStrokeColor(c, red);
CGContextBeginPath(c);
CGContextMoveToPoint(c, 50.0f, 50.0f);
CGContextAddLineToPoint(c, 100.0f, 100.0f);
CGContextAddLineToPoint(c, 120.0f, 80.0f);
CGContextAddLineToPoint(c, 140.0f, 120.0f);
CGContextAddLineToPoint(c, 160.0f, 80.0f);
...
CGContextStrokePath(c) ;
}
Jus a little correction to your code:
-(void)drawLineFromPoint:(CGPoint)startPoint toPoint:(CGPoint)endPoint{
UIGraphicsBeginImageContext(YOUR_IMAGEVIEW.image.size);
[YOUR_IMAGEVIEW.image drawInRect:CGRectMake(0, 0, YOUR_IMAGEVIEW.image.size.width, YOUR_IMAGEVIEW.image.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), startPoint.x, startPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), endPoint.x, endPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
[YOUR_IMAGEVIEW setImage:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
}
YOUR_IMAGEVIEW is an outlet to your imageview you are drawing on.
As you can see - you just have to send to this method your start point and the end one! Easy as 1-2-3.
EDIT 1
How to use it? Declare a global CGPoint "startPoint";
Then say:
//___________________________________________________
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
startPoint = [touch locationInView:YOUR_IMAGEVIEW];
}
//___________________________________________________
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint nextPoint = [touch locationInView:YOUR_IMAGEVIEW];
[self drawLineFromPoint:startPoint toPoint:nextPoint];
startPoint = nextPoint;
}
EDIT 2
Here is another way to draw your points:
- (void)drawGraphMethod{
NSMutableArray *pointsArr = [[[NSMutableArray alloc]init]autorelease];
//now you should add the needed points to your array
//I have no idea what graph do you have, so I just
//put there some random points that will look like a graph
[pointsArr addObject:[NSValue valueWithCGPoint:CGPointMake(30.0, 10.0)]];
[pointsArr addObject:[NSValue valueWithCGPoint:CGPointMake(40.0, 26.0)]];
[pointsArr addObject:[NSValue valueWithCGPoint:CGPointMake(70.0, 55.0)]];
[pointsArr addObject:[NSValue valueWithCGPoint:CGPointMake(80.0, 88.0)]];
[pointsArr addObject:[NSValue valueWithCGPoint:CGPointMake(90.0, 33.0)]];
[pointsArr addObject:[NSValue valueWithCGPoint:CGPointMake(130.0, 100.0)]];
//well, you can store only objects, that's why I used NSValue.
//it's not hard to cenvert it back
//now parse the array
for (int i = 0; i < pointsArr.count-1; i++) {
CGPoint startPoint = [[pointsArr objectAtIndex:i] CGPointValue];
CGPoint nextPoint = [[pointsArr objectAtIndex:i+1] CGPointValue];
[self drawLineFromPoint:startPoint toPoint:nextPoint];
}
}

Drawing paint brush using UIBezierPath is working with uiview but not working with uiimageview?

i am making paint brush using UIBezierPath using following code.
.h File
#interface MyLineDrawingView : UIView
{
UIBezierPath *myPath;
UIColor *brushPattern;
}
#end
.m File
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"0010.png"]];
myPath=[[UIBezierPath alloc]init];
myPath.lineWidth=30;
brushPattern=[UIColor redColor];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
[brushPattern setStroke];
[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
// [myPath strokeWithBlendMode:kCGBlendModeSaturation alpha:1.0];
// Drawing code
//[myPath stroke];
}
#pragma mark - Touch Methods
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath moveToPoint:[mytouch locationInView:self]];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];
}
It is working well with UIView. As you can see in above custom class inherits from UIVIew. But when i am subclassing UIImageView instead of UIView I am not able to draw anything. Toches method are called but it draws nothing on the screen. Does anyone know what is wrong with this or how can i resolve this?
The reason that i want to change it to UIImageView from UIView is I want to set the image and change the color. When I use UIView and use
self.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"0010.png"]];
the image does not fit to view. Only part of the whole image is visible. Even if i change the content mode to UIViewContentModeScaleToFill then also whole image is not visible.
From the UIImageView documentation:
The UIImageView class is optimized to draw its images to the display. UIImageView will not call drawRect: a subclass. If your subclass needs custom drawing code, it is recommended you use UIView as the base class.
In short, drawRect can't be used in UIImageView subclasses. Add an image view as a subview or draw the image as part of your drawRect instead.
It can be done using following code:
#interface Canvas : UIImageView {
CGPoint location;
}
#property CGPoint location;
.m file
#synthesize location;
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
self.location = [touch locationInView:self];
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentLocation = [touch locationInView:self];
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
//CGContextSetBlendMode(ctx, kCGBlendModeOverlay);
[self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
CGContextSetLineWidth(ctx, 5.0);
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
//CGContextSetBlendMode(ctx, kCGBlendModeOverlay);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, location.x, location.y);
CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
CGContextStrokePath(ctx);
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
location = currentLocation;
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentLocation = [touch locationInView:self];
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
// CGContextSetBlendMode(ctx, kCGBlendModeOverlay);
[self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, 5.0);
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, location.x, location.y);
CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
CGContextStrokePath(ctx);
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
location = currentLocation;
}

how to draw airbrush on UIImageView?

I am a beginner working on paint application on iphone.
adding new tool for my iphone App called airbrush...
which will spray on UIImageView. can any one help me out how to work with it.
Logic for air brush.........
- (UIBezierPath *)pathFromPoint:(CGPoint)start toPoint:(CGPoint)end {
CGFloat lineWidth=10;
redrawRect = CGRectMake(end.x-lineWidth,end.y-lineWidth,lineWidth*2,lineWidth*2);
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:redrawRect];
NSInteger i, x, y;
NSInteger modNumber =4*(int)lineWidth;
for (i = 0; i < (lineWidth*lineWidth)/2; i++) {
do {
x = (random() % modNumber)+end.x - 2*lineWidth;
y = (random() % modNumber)+end.y - 2*lineWidth;
} while (![circle containsPoint:CGPointMake(x,y)]);
[bezierPath appendPath:[UIBezierPath bezierPathWithRect:CGRectMake(x,y,0.5,0.5)]];
}
return bezierPath;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:self.view];
currentPoint.y -=20;
[self drawCircle];
}
-(void)drawCircle{
UIGraphicsBeginImageContext(self.drawImage.frame.size);
[drawImage.image drawInRect:CGRectMake(0,0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
CGContextSetLineWidth(UIGraphicsGetCurrentContext(),10);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
UIBezierPath *path=[self pathFromPoint:currentPoint
toPoint:currentPoint];
[path stroke];
lastPoint = currentPoint;
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
I think you might be looking for CGContextBeginPath and its related functions. I am not quite sure how to define a new stroke but I imagine it can be handled with something like [UIColor colorFromImage:myImage]. You should look into Quartz 2D, try looking here.
/Thomas