UIView drawRect: when you draw a line, the rect area will be clear so the previous drawing is gone -


it quite hard tell upload image show problem: http://i42.tinypic.com/2eezamo.jpg

basically in drawrect, draw line touchesmoved finger touches , call "needsdisplayinrect" redraw. found first line done, second line clear rect part, previouse drawing gone.

here implementation:

enter code here  -(void) drawrect:(cgrect)rect{ //[super drawrect: rect]; cgcontextref context = uigraphicsgetcurrentcontext(); [self drawsquiggle:squiggle at:rect incontext:context]; }  - (void)drawsquiggle:(squiggle *)squiggle at:(cgrect) rect incontext:(cgcontextref)context {     cgcontextsetblendmode(context, kcgblendmodemultiply); uicolor *squigglecolor = squiggle.strokecolor; // squiggle's color cgcolorref colorref = [squigglecolor cgcolor]; // cgcolor cgcontextsetstrokecolorwithcolor(context, colorref);          nsmutablearray *points = [squiggle points]; // points squiggle // retrieve nsvalue object , store value in firstpoint cgpoint firstpoint; // declare cgpoint [[points objectatindex:0] getvalue:&firstpoint]; // move point cgcontextmovetopoint(context, firstpoint.x, firstpoint.y); // draw line each point next in order  (int = 1; < [points count]; i++) {     nsvalue *value = [points objectatindex:i]; // next value     cgpoint point; // declare new point     [value getvalue:&point]; // store value in point      // draw line new point     cgcontextaddlinetopoint(context, point.x, point.y); } // end cgcontextstrokepath(context);  } 

i try implement seems doesn't work expected. have implemented view draw square every time setneedsdisplay called on view. put cgimageref image , cgbitmapcontextref context ivar , allocate them when view inits.

#import "view.h"   @implementation view   #define bitspercomponent 8 #define bitsperpixel 4*bitspercomponent #define bytesperpixel 4   - (id)initwithframe:(cgrect)frame { if ((self = [super initwithframe:frame])) {     lastpoint = cgpointmake(50, 50);     size_t width = frame.size.width;     size_t height = frame.size.height;      cfmutabledataref data = cfdatacreatemutable( null , width * height * bytesperpixel );       cgdataproviderref provider = cgdataprovidercreatewithcfdata( data );     cgcolorspaceref colorspace = cgcolorspacecreatedevicergb();      /*cgimageref - ivar */ image = cgimagecreate(width, height, bitspercomponent, bitsperpixel, width*bytesperpixel, colorspace, kcgimagealphapremultipliedlast, provider, null, no, kcgrenderingintentdefault);     /*cgbitmapcontextref - ivar */ context = cgbitmapcontextcreate(cfdatagetmutablebyteptr(data), width, height, bitspercomponent, width*bytesperpixel , colorspace, kcgimagealphapremultipliedlast);      cfrelease( data ); // retained provider think     cgdataproviderrelease( provider ); // retained image     cgcolorspacerelease(colorspace); } return self; }  - (void)drawrect:(cgrect)rect { nslog(@"test"); cgcontextsetfillcolorwithcolor(context, [[uicolor bluecolor] cgcolor]); cgcontextfillrect(context, cgrectmake(lastpoint.x, lastpoint.y, 100, 100)); lastpoint = cgpointmake( ((int)lastpoint.x+50) % 380 , ((int)lastpoint.y+50) % 220 ); cgcontextdrawimage(uigraphicsgetcurrentcontext(), cgrectmake(0, 0, 100, 100),image); }  - (void)dealloc { [super dealloc]; }   @end 

the graphics context returned uigraphicsgetcurrentcontext in drawrect: owned system , cleared @ start of drawing pass. create own cgcontext record drawing , render drawrect: context needed.

create paired cgbitmapcontext , cgimage refer same cgdataprovider , have same size, color space , other properties. draw context when , how see fit, in drawrect: use cgcontextdrawimage display it.

or if doing series of lines, can build cgmutablepath , render system context.

edit:

look @ cgimagecreate , cgbitmapcontextcreate in respective header files. of arguments same. bitmap context expects raw data pointer, while image expects cgdataprovider. here rough sketch of code like. in end can draw context use image render context.

size_t width = 400; size_t height = 300; cfmutabledataref data = cfdatacreatemutable( null , width * height * 4 ); // 4 bytes per pixel cgdataproviderref provider = cgdataprovidercreatewithcfdata( data ); cgimageref image = cgimagecreate( width , height , ... , provider , ... ); cgbitmapcontextref context = cgbitmapcontextcreate( cfdatagetmutablebyteptr( data ) , width , height , ... ); cfrelease( data ); // retained provider think cgdataproviderrelease( provider ); // retained image 

there lot of blanks fill in, should started. use malloc instead of cfdata buffer.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -