ios - How to draw a triangle over a UIImage in a UIImageView -
i'm having trouble drawing atop image in uiimageview, i've looked @ "draw image on uiimage" help. here's scenario: have uipopovercontroller uitableview in , want display triangle pointing when user @ bottom of scrollable table view.
my code:
- (uiimage *) drawtriangleinviewforsize:(cgsize)sizeofview imagetodrawon:(uiimage *)underimage isattop:(bool)top{ cgpoint firstpoint; cgpoint secondpoint; cgpoint thirdpoint; if(!top){ //i want draw equilateral triangle (side length 10) in center of image in //the imageview, these coordinates using. firstpoint = cgpointmake(underimage.size.width * 0.5 + 5, underimage.size.height * 0.5 - 5); secondpoint = cgpointmake((firstpoint.x - 10), firstpoint.y); thirdpoint = cgpointmake(underimage.size.width * 0.5, underimage.size.width * 0.5 + 5); } */ **disregard** else{ firstpoint = cgpointmake(sizeofview.width * 0.5 + 5, self.tableviewchoices.rowheight * 0.5 - 5); secondpoint = cgpointmake((firstpoint.x - 10), firstpoint.y); thirdpoint = cgpointmake(sizeofview.width * 0.5, self.tableviewchoices.rowheight * 0.5 + 5); }*/ //get size of image drawinrect: method cgfloat imageviewwidth = sizeofview.width; cgfloat imageviewheight = self.tableviewchoices.rowheight; //set graphics context size of image uigraphicsbeginimagecontextwithoptions(underimage.size, yes, 0.0); [underimage drawinrect:cgrectmake(0.0, 0.0, imageviewwidth, imageviewheight)]; //set line attributes cgcontextref ctx = uigraphicsgetcurrentcontext(); cgcontextsetstrokecolorwithcolor(ctx, [uicolor blackcolor].cgcolor); cgcontextsetfillcolorwithcolor(ctx, [uicolor clearcolor].cgcolor); cgcontextsetlinewidth(ctx, 0.05); uigraphicspushcontext(ctx); //draw triangle cgcontextbeginpath(ctx); cgcontextmovetopoint(ctx, firstpoint.x, firstpoint.y); cgcontextaddlinetopoint(ctx, secondpoint.x, secondpoint.y); cgcontextaddlinetopoint(ctx, thirdpoint.x, thirdpoint.y); cgcontextaddlinetopoint(ctx, firstpoint.x, firstpoint.y); cgcontextclosepath(ctx); uigraphicspopcontext(); //get image uiimage *rslt = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); return rslt; }
i can't quite seem draw triangle uiimage in imageview, end result either black imageview (the uiimage white.png), or 1 line @ top of imageview (depending on whether use drawinrect:
or drawatpoint:
, coordinates use). need draw triangle pointing up, not hard thing, , looks i'm doing "by book."
your code looks complicated kind of task. why don't use simple uiimageview
triangle image ever want? show when table @ bottom , that's all.
note, when you're implementing delegate uitableview
you're able catch it's uiscrollview
's delegate messages such scrollview:didenddecelerating:
, on.
regarding code you've missed actual path draw:
cgcontextaddpath(context, trianglepath); cgcontextfillpath(context);
to make possible should use path-related functions. using cgcontext
-related functions seems improper since such approach may remove old path. better way create own mutable path in context, draw triangle in , draw path context functions mentioned above.
Comments
Post a Comment