drawing - How erase a line draw in iphone(CGGraphics)? -
drawing - How erase a line draw in iphone(CGGraphics)? -
i working on iphone application of drawing, user can draw line , can remove can body help me how remove line or drawing?
code draw
- (void)drawrect:(cgrect)rect { float newheight; float newwidth; if (!mydrawing) { mydrawing = [[nsmutablearray alloc] initwithcapacity:0]; } cgcontextref ctx = uigraphicsgetcurrentcontext(); if (mypic != null) { float ratio = mypic.size.height/460; if (mypic.size.width/320 > ratio) { ratio = mypic.size.width/320; } newheight = mypic.size.height/ratio; newwidth = mypic.size.width/ratio; [mypic drawinrect:cgrectmake(0,0,newwidth,newheight)]; } if ([mydrawing count] > 0) { cgcontextsetlinewidth(ctx, 5); (int = 0 ; < [mydrawing count] ; i++) { nsarray *thisarray = [mydrawing objectatindex:i]; if ([thisarray count] > 2) { float thisx = [[thisarray objectatindex:0] floatvalue]; float thisy = [[thisarray objectatindex:1] floatvalue]; cgcontextbeginpath(ctx); cgcontextmovetopoint(ctx, thisx, thisy); (int j = 2; j < [thisarray count] ; j+=2) { thisx = [[thisarray objectatindex:j] floatvalue]; thisy = [[thisarray objectatindex:j+1] floatvalue]; cgcontextaddlinetopoint(ctx, thisx,thisy); } cgcontextstrokepath(ctx); } } } } - (void) touchesbegan:(nsset *)touches withevent:(uievent *)event { [mydrawing addobject:[[nsmutablearray alloc] initwithcapacity:4]]; cgpoint curpoint = [[touches anyobject] locationinview:self]; [[mydrawing lastobject] addobject:[nsnumber numberwithfloat:curpoint.x]]; [[mydrawing lastobject] addobject:[nsnumber numberwithfloat:curpoint.y]]; } - (void) touchesmoved:(nsset *)touches withevent:(uievent *)event { cgpoint curpoint = [[touches anyobject] locationinview:self]; [[mydrawing lastobject] addobject:[nsnumber numberwithfloat:curpoint.x]]; [[mydrawing lastobject] addobject:[nsnumber numberwithfloat:curpoint.y]]; [self setneedsdisplay]; } - (void) touchesended:(nsset *)touches withevent:(uievent *)event { cgpoint curpoint = [[touches anyobject] locationinview:self]; [[mydrawing lastobject] addobject:[nsnumber numberwithfloat:curpoint.x]]; [[mydrawing lastobject] addobject:[nsnumber numberwithfloat:curpoint.y]]; [self setneedsdisplay]; } -(void)canceldrawing { [mydrawing removeallobjects]; [self setneedsdisplay]; }
you can utilize cgcontextsetstrokecolorwithcolor
, set color of background of view while drawing line erasing line.
and if want provide user undo facility while drawing can create next function that,
-(void)undodrawing { [mydrawing removelastobject]; [self setneedsdisplay]; }
hope helps...
iphone drawing line cgcontextdrawimage
Comments
Post a Comment