Please see the attached image. I have created an app in which user can draw with multiple images but when I am using the Clear Color in order to erase the drawing it is showing the black border around the erased path. I have used UIBezierPath for drawing. Is this an issue with UIBezierPath or it can be resolved by any other way? Please let me know if you want code snippet! I will post that here.
Edit: Code has been added! Please ignore the irrelevant code.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if(isFirstTapp)
{
isFirstTapp=NO;
[self.spooleteView removeHandView];
}
isdraw=YES;
mouseSwapped=NO;
UITouch *touch = [touches anyObject];
lastPoint=[touch locationInView:self];
//UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[currentPath moveToPoint:p];
currentPath.flatness = 10.0;
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
UIBezierPath *myPath=[[UIBezierPath alloc]init];
myPath.lineCapStyle=kCGLineJoinRound;
[dict setObject:myPath forKey:#"Path"];
colorTag= [[AppHelper userDefaultsForKey:#"ColorTag"]intValue];
self.selectedColor = [brushColor objectAtIndex:colorTag];
if(isErase)
{
myPath.lineWidth=30.0;
self.selectedColor = [UIColor whiteColor];
}
else
{
if([mode isEqualToString:#"morsecode"])
myPath.lineWidth=10.0;
else
myPath.lineWidth=5.0;
}
//[dict setObject:[brushColor objectAtIndex:colorTag] forKey:#"Colors"];
[dict setObject:self.selectedColor forKey:#"Colors"];
[myArr addObject:dict];
currentPath=myPath;
currentPath.flatness = 0.1;
currentPath.lineJoinStyle = kCGLineJoinRound;
currentDict=dict;
[myPath moveToPoint:lastPoint];
[myPath release];
[dict release];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if(!istapped)
{
if([mode isEqualToString:#"morsecode"])
{
mouseSwapped=YES;
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
currentPoint=[mytouch locationInView:self];
[currentPath addLineToPoint:currentPoint];
[self setNeedsDisplay];
}
}
// UITouch *touch = [touches anyObject];
// CGPoint p = [touch locationInView:self];
// [currentPath addLineToPoint:p];
// [self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(!istapped)
{
mouseSwapped=YES;
UITouch *objTouch=[[touches allObjects] objectAtIndex:0];
if (!mouseSwapped&&(objTouch.tapCount==1))
{
lastPoint=currentPoint;
}
}
if(isErase)
{
if ([myArr count]>50)
{
[myArr removeAllObjects];
}
}
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[currentPath addLineToPoint:p];
[self drawBitmap]; // (3)
[self setNeedsDisplay];
[currentPath removeAllPoints]; //(4)
}
- (void)drawRect:(CGRect)rect
{
[self.incrementalImage drawInRect:rect]; // (3)
[currentPath stroke];
//[self.incrementalImage drawInRect:rect];
for (NSMutableDictionary *dictionary in myArr)
{
UIBezierPath *_path = [dictionary objectForKey:#"Path"];
self.selectedColor = [dictionary objectForKey:#"Colors"];
[self.selectedColor setStroke];
[_path stroke];
[_path strokeWithBlendMode:kCGBlendModeCopy alpha:1.0];
_path.lineCapStyle = kCGLineJoinRound;
if([mode isEqualToString:#"morsecode"])
{
if (!isErase)
{
const float p[2] = {1,15.9};
[_path setLineDash:p count:2 phase:0.9];
}
else
{
self.selectedColor = [UIColor whiteColor];
}
}
}
}
- (void)drawBitmap // (3)
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
[self.selectedColor setStroke];
if (!self.incrementalImage) // first draw; paint background white by ...
{
UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object
[[UIColor clearColor] setFill];
[rectpath fill]; // filling it with white
}
[self.incrementalImage drawAtPoint:CGPointZero];
[currentPath stroke];
self.incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
I have found the reason of that issue. It was because of the second line of the following function.
- (void)drawRect:(CGRect)rect
{
[self.incrementalImage drawInRect:rect]; // (3)
[currentPath stroke];
i.e., [currentPath stroke];
I have commented that and now there is no black border around the path.
I hope this will help other.
Related
I wanna draw the transparent area with brush, but my code work not very well.I think someone can help me here. My code :
// Handles the start of a touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGRect bounds = [self bounds];
UITouch *touch = [[event touchesForView:self] anyObject];
if (![image isPointTransparent:[touch locationInView:self]]
|| ![image isPointTransparent:[touch previousLocationInView:self]])
{
return;
}
firstTouch = YES;
// Convert touch point from UIView referential to OpenGL one (upside-down flip)
location = [touch locationInView:self];
location.y = bounds.size.height - location.y;
}
// Handles the continuation of a touch.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGRect bounds = [self bounds];
UITouch *touch = [[event touchesForView:self] anyObject];
if (![image isPointTransparent:[touch locationInView:self]]
|| ![image isPointTransparent:[touch previousLocationInView:self]])
{
return;
}
// Convert touch point from UIView referential to OpenGL one (upside-down flip)
if (firstTouch)
{
firstTouch = NO;
previousLocation = [touch previousLocationInView:self];
previousLocation.y = bounds.size.height - previousLocation.y;
}
else
{
location = [touch locationInView:self];
location.y = bounds.size.height - location.y;
previousLocation = [touch previousLocationInView:self];
previousLocation.y = bounds.size.height - previousLocation.y;
}
// Render the stroke
[self renderLineFromPoint:previousLocation toPoint:location];
}
// Handles the end of a touch event when the touch is a tap.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGRect bounds = [self bounds];
UITouch *touch = [[event touchesForView:self] anyObject];
if (![image isPointTransparent:[touch locationInView:self]] || ![image isPointTransparent:[touch previousLocationInView:self]])
{
return;
}
if (firstTouch)
{
firstTouch = NO;
previousLocation = [touch previousLocationInView:self];
previousLocation.y = bounds.size.height - previousLocation.y;
[self renderLineFromPoint:previousLocation toPoint:location];
}
}
The important thing to know is that you should do your actual drawing in the drawRect: of your UIView. So the renderLineFromPoint:toPoint: method in your code should just be building up an array of lines and telling the view to redraw each time, something like this:
- (void)renderLineFromPoint:(CGPoint)from toPoint:(CGPoint)to
{
[lines addObject:[Line lineFrom:from to:to]];
[self setNeedsDisplay];
}
This assumes that you have a Line class which has 2 CGPoint properties. Your drawRect: may look something like this:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 0.0f, 0.0f, 0.0f, 1.0f);
for (Line *line in lines) {
CGContextMoveToPoint(context, line.from.x, line.from.y);
CGContextAddLineToPoint(context, line.to.x, line.to.y);
CGContextStrokePath(context);
}
}
If you do it like this (without OpenGL) there is no need to flip the y-axis.
The only thing left is to implement the isPointTransparent: method. It's difficult to know from your code how this should work.
I just want to draw line between 2 different points in my same view which is of type ViewController.. I have written the following code but it's not displaying line.. Please help me solve this issue..
- (void)drawRect:(CGRect)rect
{
[brushPattern setStroke];
[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
// CGPoint *point=mytouch
[myPath moveToPoint:[mytouch locationInView:self.view]];
CGPoint pos = [mytouch locationInView: self.view];
NSLog(#"Position of touch: %.3f, %.3f", pos.x, pos.y);
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath addLineToPoint:[mytouch locationInView:self.view]];
[self.view setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
CGPoint pos = [mytouch locationInView: self.view];
NSLog(#"Position of touch: %.3f, %.3f", pos.x, pos.y);
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
currentPoint = [touch locationInView:self.image];
UIGraphicsBeginImageContext(self.view.frame.size);
if (!(last.x == 0 && last.y == 0)) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 2.0);
CGContextMoveToPoint(context, currentPoint.x,currentPoint.y);
CGContextAddLineToPoint(context, last.x, last.y);
CGContextStrokePath(context);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapButt);
[image setNeedsDisplay];
CGContextFlush(UIGraphicsGetCurrentContext());
self.image.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
last = currentPoint;
NSLog(#"the current point is %f,%f",currentPoint.x,currentPoint.y);
NSLog(#"the current point is %f,%f",last.x,last.y);
}
Here is my code for free hand drawing. But when i draw the path, previous path is disappeared. I am not able to figure it out why is it happening so. Can any body help me. Here is my code.
- (void)drawRect:(CGRect)rect
{
for (NSMutableDictionary *dictionary in pathArray) {
UIBezierPath *_path = [dict objectForKey:#"Path"];
UIColor *_colors = [dict objectForKey:#"Colors"];
[_colors setStroke];
_path.lineCapStyle = kCGLineCapRound;
[_path stroke];
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
isEdited=YES;
myPath=[[UIBezierPath alloc]init];
myPath.lineWidth=lineWidths;
CGPoint touchPoint = [[touches anyObject] locationInView:self];
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath moveToPoint:[mytouch locationInView:self]];
[myPath addLineToPoint:CGPointMake(touchPoint.x+1, touchPoint.y+1)];
[dict setObject:myPath forKey:#"Path"];
[dict setObject:brushPattern forKey:#"Colors"];
[pathArray addObject:dict];
[self setNeedsDisplay];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];
}
You should create myPath and dict localy in touchesBegan: each time it's fired. Ditch their class-wide definitions.
For simpler (faster) performance you can still have class-wide currentPath and currentDict
ivars for usage in touchesMoved:
EDIT: code would look something like this:
//currentPath declared as an iVar of UIBezierPath* type
//currentDict declared as an iVar of NSMutableDictionary* type
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
isEdited=YES;
UIBezierPath *myPath=[[UIBezierPath alloc]init]; //locally created
myPath.lineWidth=lineWidths;
CGPoint touchPoint = [[touches anyObject] locationInView:self];
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath moveToPoint:[mytouch locationInView:self]];
[myPath addLineToPoint:CGPointMake(touchPoint.x+1, touchPoint.y+1)];
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init]; //locally created
[dict setObject:myPath forKey:#"Path"];
[dict setObject:brushPattern forKey:#"Colors"];
[pathArray addObject:dict];
[self setNeedsDisplay];
currentPath = myPath;
currentDict = dict;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[currentPath addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];
}
1) Create a UIImage *currentImage instance variable in your drawing view.
2) in drawRect method of your view put the following line:
[currentImage drawInRect:self.bounds];
3) In touchesEnded method put the following code:
CGRect rect = self.bounds;
UIGraphicsBeginImageContext(rect.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
currentImage = tempImage;
I am building an iOS app for drawing first time. Now i am able to draw lines and curves using core graphics but unable UNDO the drawing. I am saving all the points when drawing and try trying to reuse them for UNDO and REDO, but with no success. Can anyone help to know what i am doing wrong?
Any help is highly appreciated.
Here is my code
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[tempPathArray removeAllObjects];
UITouch *touch = [touches anyObject];
previousPoint1 = [touch previousLocationInView:self];
previousPoint2 = [touch previousLocationInView:self];
currentPoint = [touch locationInView:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
previousPoint2 = previousPoint1;
previousPoint1 = [touch previousLocationInView:self];
currentPoint = [touch locationInView:self];
// calculate mid point
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, mid1.x, mid1.y);
CGPathAddQuadCurveToPoint(path, NULL, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGRect bounds = CGPathGetBoundingBox(path);
CGPathRelease(path);
drawBox = bounds;
//Pad our values so the bounding box respects our line width
drawBox.origin.x -= self.lineWidth * 2;
drawBox.origin.y -= self.lineWidth * 2;
drawBox.size.width += self.lineWidth * 4;
drawBox.size.height += self.lineWidth * 4;
UIGraphicsBeginImageContext(drawBox.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
curImage = UIGraphicsGetImageFromCurrentImageContext();
[curImage retain];
UIGraphicsEndImageContext();
[tempPathArray addObject:[NSValue valueWithCGRect:drawBox]];
[self setNeedsDisplayInRect:drawBox];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[pathArray addObject:tempPathArray];
NSLog(#"path array count %d", [pathArray count]);
}
- (void)drawRect:(CGRect)rect
{
NSLog(#"draw rect");
[curImage drawAtPoint:CGPointMake(0, 0)];
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
context = UIGraphicsGetCurrentContext();
[self.layer renderInContext:context];
CGContextMoveToPoint(context, mid1.x, mid1.y);
// Use QuadCurve is the key
CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextStrokePath(context);
[super drawRect:rect];
}
-(void)undoButtonClicked
{
KidsPhotoBookAppDelegate *appDelegate = (KidsPhotoBookAppDelegate*)[[UIApplication sharedApplication]delegate];
if([pathArray count]>0){
[bufferArray addObject:[pathArray lastObject]];
[pathArray removeLastObject];
for (NSMutableArray *tempArray in pathArray) {
for (int i = 0; i < [tempArray count]; i++) {
CGRect draw = [[tempArray objectAtIndex:i] CGRectValue];
[self setNeedsDisplayInRect:draw];
}
}
}
}
Try this:
Draw a line.
Draw another line back over the first line.
Undo.
I suspect you'll see it mostly workâthe part of the second line that overlapped the first line will disappear, and only the part that didn't will remain.
The problem is that you're setting needs display on the portions of the drawing that you still have, but not the portion that you just vacated. This is exactly the inverse of what you should be doing: The portions you still have don't need to be redrawn, because they haven't changed, whereas the portion you vacated does need to be redrawn because it has changed.
So, set needs display in the area of the path you're removing, just before you remove it. You shouldn't need to set needs display of anything else.
I'm trying to implement a very simple drawing view in my app. This is only a small part of my app but it's turning into a real hassle. This is what I have so far, but all it's displaying right now is morse code like dots and lines.
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *savePath = [NSString stringWithFormat:#"%#/notePadImage.jpg",docsPath];
NSData *data = [NSData dataWithContentsOfFile:savePath];
UIImage *image = [UIImage imageWithData:data];
if (image == nil) {
NSString *pathToBlank = [[NSBundle mainBundle]pathForResource:#"blankNotePadPage" ofType:#"png"];
NSData *data = [NSData dataWithContentsOfFile:pathToBlank];
image = [UIImage imageWithData:data];
}
arrayOfTouches = [[NSMutableArray alloc] initWithCapacity:10];
self.drawImage.image = image;
mouseMoved = 0;
[self.view bringSubviewToFront:closeButton];
[self.view bringSubviewToFront:clearButton];
self.timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:#selector(drawIt) userInfo:nil repeats:YES];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
[arrayOfTouches addObject:touch];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
[arrayOfTouches addObject:touch];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
}
-(void) drawIt {
NSMutableArray *tempArray = [NSMutableArray arrayWithArray:arrayOfTouches];
[arrayOfTouches removeAllObjects];
if ([tempArray count]>1) {
[arrayOfTouches removeAllObjects];
CGPoint point1 = [[tempArray objectAtIndex:0] previousLocationInView:self.view];;
CGPoint point2;
CGPoint point3;
for (int i = 0; i < [tempArray count]-1;i = i+1) {
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(), 3.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), point1.x, point1.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), point2.x, point2.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.view bringSubviewToFront:closeButton];
[self.view bringSubviewToFront:clearButton];
point1 = point2;
}
}
}
One of my apps also needed some simple drawing. Here is a slightly modified version of it. It works basically like hotpaw2 describes. I created a "canvas" view that handles all the drawing and I just add it wherever it's needed.
The speed is fine for my purposes.
CanvasView.h:
#interface CanvasView : UIView {
NSMutableArray *points;
}
#property (nonatomic, retain) NSMutableArray *points;
#end
CanvasView.m:
#import "CanvasView.h"
#implementation CanvasView
#synthesize points;
- (id) initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor blueColor];
}
return self;
}
-(void)drawRect:(CGRect)rect
{
if (self.points.count == 0)
return;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0); //white
CGContextSetLineWidth(context, 1.0);
CGPoint firstPoint = [[self.points objectAtIndex:0] CGPointValue];
CGContextBeginPath(context);
CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);
int i = 1;
while (i < self.points.count)
{
CGPoint nextPoint = [[self.points objectAtIndex:i] CGPointValue];
if (nextPoint.x < 0 && nextPoint.y < 0)
{
CGContextDrawPath(context, kCGPathStroke);
if (i < (self.points.count-1))
{
CGContextBeginPath(context);
CGPoint nextPoint2 = [[self.points objectAtIndex:i+1] CGPointValue];
CGContextMoveToPoint(context, nextPoint2.x, nextPoint2.y);
i = i + 2;
}
else
i++;
}
else
{
CGContextAddLineToPoint(context, nextPoint.x, nextPoint.y);
i++;
}
}
CGContextDrawPath(context, kCGPathStroke);
}
-(void)dealloc
{
[points release];
[super dealloc];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event touchesForView:self] anyObject];
CGPoint location = [touch locationInView:self];
if (self.points == nil)
{
NSMutableArray *newPoints = [[NSMutableArray alloc] init];
self.points = newPoints;
[newPoints release];
}
[self.points addObject:[NSValue valueWithCGPoint:(location)]];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event touchesForView:self] anyObject];
CGPoint location = [touch locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:(location)]];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event touchesForView:self] anyObject];
CGPoint location = [touch locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:(location)]];
CGPoint endPoint = CGPointMake(-99, -99); //"end of path" indicator
[self.points addObject:[NSValue valueWithCGPoint:(endPoint)]];
[self setNeedsDisplay];
}
#end
Adding the canvasView where it's needed:
CanvasView *cv = [[CanvasView alloc] initWithFrame:CGRectMake(0, 0, 320, 640)];
[self.view addSubview:cv];
[cv release];
Don't draw while handling touches. It will slow down the touch handler so much you might get the connect-the-dots-effect you are seeing.
Save the touch coordinates in an array and plan to draw them later.
Look at some simple animation tutorials for how to draw in a drawRect based on a setNeedsDisplay called by an animation UITimer or CADisplayLink. Draw all your line segments there at a more suitable rate.