ios - How to optimize CPU and RAM usage on a UIView which uses `drawInRect:blendMode:`? -
i'm developing drawing app lets user draw 1 uibezierpath once on image has been drawn in uiview drawinrect:blendmode
in drawrect
method , it's uses on 80% of cpu , 55mb of ram , when traced down in time profiler, drawinrect:blendmode
method causing of cpu usage. have done few optimizations further optimizations can both drawing uiimage being drawn using [image drawinrect:rect];
, uibezierpath user draws on top of it? in advance.
@interface insideview(){ cgrect imagerect; cgrect targetbounds; } @property (strong, nonatomic) nsmutablearray *strokes; @property (nonatomic, strong) uiimage * img; @property (nonatomic, strong) uibezierpath *drawingpath; @end @implementation insideview -(void)drawrect:(cgrect)rect{ targetbounds = self.layer.bounds; imagerect = avmakerectwithaspectratioinsiderect(self.img.size,targetbounds); [self.img drawinrect:imagerect]; (int i=0; < [self.strokes count]; i++) { uibezierpath* tmp = (uibezierpath*)[self.strokes objectatindex:i]; [[uicolor whitecolor] setstroke]; [tmp stroke]; } } - (cgrect)segmentboundsfrom:(cgpoint)point1 to:(cgpoint)point2 { cgrect dirtypoint1 = cgrectmake(point1.x-10, point1.y-10, 50, 50); cgrect dirtypoint2 = cgrectmake(point2.x-10, point2.y-10, 50, 50); return cgrectunion(dirtypoint1, dirtypoint2); } - (void)touchesbegan:(nsset *)touches withevent:(uievent *)event{ self.drawingpath = [uibezierpath bezierpath]; self.drawingpath.linewidth = 10; self.drawingpath.linecapstyle = kcglinecapround; self.drawingpath.linejoinstyle = kcglinejoinround; [self.drawingpath movetopoint:[[touches anyobject] locationinview:self]]; [self.strokes addobject:self.drawingpath]; } - (void)touchesmoved:(nsset *)touches withevent:(uievent *)event{ //dirty rect calculations cgpoint prevpoint = cgpathgetcurrentpoint(self.drawingpath.cgpath); cgpoint point = [[touches anyobject] locationinview:self]; cgrect dirty = [self segmentboundsfrom:prevpoint to:point]; [[self.strokes lastobject] addlinetopoint:point]; [self setneedsdisplayinrect:dirty]; } - (void)touchesended:(nsset *)touches withevent:(uievent *)event{ [[self.strokes lastobject] addlinetopoint:[[touches anyobject] locationinview:self]]; [self setneedsdisplay]; }
Comments
Post a Comment