I'm trying to figure out how you can flatten or merge alot of dynamicly drawn lines in Quartz 2D,
I'm drawing random lines over time on my stage, I add new line coordinates each time to an array and draw the array (with my drawRect and I push a new line in my array with a timer and a setNeedDisplay to acctually redraw all the previous lines plus the new one)
now the PROBLEM: after a while it starts to get slower and slower because the arrays are getting very long, so I though I should merge the coordinates into a flat image or something and clear the arrays to keep it memory healthy, but how do I do this?
This is my current workflow:
Call timer
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(drawdrawdraw) userInfo:nil repeats:YES];
Refresh the drawRect in my "drawdrawdraw" function
-(void)drawdrawdraw{ [self setNeedsDisplay]; }
my drawRect
-(void)drawRect:(CGRect)rect{ viewContext = UIGraphicsGetCurrentContext(); int count = [mijnArray_r_x count]; float r_x = (float)(random() % 768); [mijnArray_r_x insertObject:[NSNumber numberWithFloat:r_x] atIndex:count]; float r_y = (float)(random() % 1004); [mijnArray_r_y insertObject:[NSNumber numberWithFloat:r_y] atIndex:count]; float r_w = (float)(random() % 100); [mijnArray_r_w insertObject:[NSNumber numberWithFloat:r_w] atIndex:count]; float r_a = (float)(random() % 100); [mijnArray_r_a insertObject:[NSNumber numberWithFloat:r_a] atIndex:count]; CGContextSetLineWidth(viewContext, 2.0); CGContextSetStrokeColorWithColor(viewContext, [UIColor blackColor].CGColor); for (int k = 0; k <= count; k++) { float temp_x = [[mijnArray_r_x objectAtIndex: k] floatValue]; float temp_y = [[mijnArray_r_y objectAtIndex: k] floatValue]; float temp_w = [[mijnArray_r_w objectAtIndex: k] floatValue]; float temp_a = [[mijnArray_r_a objectAtIndex: k] floatValue]; CGPoint pointpointpoint = CGPointMake(temp_x, temp_y); CGPoint pointpointpointpoint = CGPointMake(temp_w, temp_a); CGContextMoveToPoint(viewContext, pointpointpoint.x, pointpointpoint.y); CGContextAddLineToPoint(viewContext, pointpointpoint.x - pointpointpointpoint.x, pointpointpoint.y + pointpointpointpoint.y); CGContextStrokePath(viewContext); 开发者_StackOverflow中文版 } } }
You can draw your lines into a UIImage by using UIGraphicsBeginImageContext(). This way, the time needed to draw will be constant, because in each iteration you're only drawing the cached image as a bitmap and the new lines you add.
- (void)drawRect:(CGRect)rect {
[self.currentImage drawInRect:[self bounds]];
UIGraphicsBeginImageContext([self bounds].size);
CGContextRef cgContext = UIGraphicsGetCurrentContext();
//Draw your lines into the cgContext here...
self.currentImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
This assumes that you have the property currentImage of type UIImage declared.
加载中,请稍侯......
精彩评论