uiimage - iOS paint optimization -
hi working on app contains taking notes drawing. followed ray wenderlich tutorials , far i've got, ended code:
- (void)touchesbegan:(nsset *)touches withevent:(uievent *)event { mouseswiped = no; uitouch *touch = [touches anyobject]; lastpoint = [touch locationinview:self]; } - (void)touchesmoved:(nsset *)touches withevent:(uievent *)event { cgfloat red,green,blue,alpha; mouseswiped = yes; uitouch *touch = [touches anyobject]; cgpoint currentpoint = [touch locationinview:self]; uigraphicsbeginimagecontext(self.mainimage.frame.size); [self.tempdrawimage.image drawinrect:cgrectmake(0, 0, self.mainimage.frame.size.width, self.mainimage.frame.size.height)]; cgcontextsetblendmode(uigraphicsgetcurrentcontext(),[self getblendmode]); cgcontextmovetopoint(uigraphicsgetcurrentcontext(), lastpoint.x , lastpoint.y ); cgcontextaddlinetopoint(uigraphicsgetcurrentcontext(), currentpoint.x , currentpoint.y ); cgcontextsetlinecap(uigraphicsgetcurrentcontext(), kcglinecapround); cgcontextsetlinewidth(uigraphicsgetcurrentcontext(), [self getbrushsize] ); [[self getpaintcolor] getred:&red green:&green blue:&blue alpha:&alpha]; cgcontextsetrgbstrokecolor(uigraphicsgetcurrentcontext(), red, green, blue, 1); cgcontextsetblendmode(uigraphicsgetcurrentcontext(),[self getblendmode]); cgcontextstrokepath(uigraphicsgetcurrentcontext()); self.tempdrawimage.image = uigraphicsgetimagefromcurrentimagecontext(); [self.tempdrawimage setalpha:[self getpaintalpha]]; uigraphicsendimagecontext(); lastpoint = currentpoint; } - (void)touchesended:(nsset *)touches withevent:(uievent *)event { cgfloat red,green,blue; if(!mouseswiped) { uigraphicsbeginimagecontext(self.mainimage.frame.size); [self.tempdrawimage.image drawinrect:cgrectmake(0, 0, self.mainimage.frame.size.width, self.mainimage.frame.size.height)]; cgcontextsetlinecap(uigraphicsgetcurrentcontext(), kcglinecapround); cgcontextsetlinewidth(uigraphicsgetcurrentcontext(), [self getbrushsize]); [[self getpaintcolor] getred:&red green:&green blue:&blue alpha:nil]; cgcontextsetrgbstrokecolor(uigraphicsgetcurrentcontext(), red, green, blue, [self getpaintalpha]); cgcontextmovetopoint(uigraphicsgetcurrentcontext(), lastpoint.x, lastpoint.y - self.mainimage.frame.origin.y); cgcontextaddlinetopoint(uigraphicsgetcurrentcontext(), lastpoint.x, lastpoint.y - self.mainimage.frame.origin.y ); cgcontextstrokepath(uigraphicsgetcurrentcontext()); cgcontextflush(uigraphicsgetcurrentcontext()); self.tempdrawimage.image = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); } uigraphicsbeginimagecontext(self.mainimage.frame.size); [self.mainimage.image drawinrect:cgrectmake(0, 0, self.mainimage.frame.size.width, self.mainimage.frame.size.height) blendmode:[self getblendmode] alpha:1.0]; [self.tempdrawimage.image drawinrect:cgrectmake(0, 0, self.mainimage.frame.size.width, self.mainimage.frame.size.height) blendmode:[self getblendmode] alpha:[self getpaintalpha]]; if(self.drawmode != draweraser) { self.mainimage.image = uigraphicsgetimagefromcurrentimagecontext(); self.tempdrawimage.image = nil; } uigraphicsendimagecontext(); mouseswiped = no; } this code working fine small frame when increased frame 2 times bigger before, unfortunately performance not good. thinking optimizing code. focused on touchesmoved method. far understand, draws whole image on context , changes , assigns context image. drawing whole image seemed overload. wondering, if can draw parts of image context , changes , draw part of context image.
you're right - redrawing whole image every time in touchesmoved bad idea. think should create , keep reference context @ beginning. in touches moved, should draw on , create image context. can use cgbitmapcontextcreate() create context instead of uigraphicsbeginimagecontext() , cgbitmapcontextcreateimage() create image context instead of uigraphicsgetimagefromcurrentimagecontext(). here documentation on how use (https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/cgbitmapcontext/reference/reference.html).
Comments
Post a Comment