i'm building an augmented reality app right now, but having some troubles with drawrect: of my UIView.
First of all i'll show you my code:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
for(AREALocation *loc in self.locations) {
CGContextSetLineWidth(context, 1.0);
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetRGBFillColor(context, 0, 0.373, 0.659, 0.5);
CGRect frame = CGRectMake(loc.point.x - 15, loc.point.y - 15, 30, 30);
CGContextAddEllipseInRect(context, frame);
CGContextDrawPath(context, kCGPathFillStroke);
CGSize textSize = [loc.name sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14]];
CGContextSetRGBStrokeColor(context, 0, 0.373, 0.659, 1.0);
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGRect textRect = CGRectMake(loc.point.x - textSize.width/2.0 - 4, loc.point.y + 20, textSize.width + 8, textSize.height + 2);
CGFloat radius = 5.0;
CGFloat minx = CGRectGetMinX(textRect);
CGFloat midx = CGRectGetMidX(textRect);
CGFloat maxx = CGRectGetMaxX(textRect);
CGFloat miny = CGRectGetMinY(textRect);
CGFloat midy = CGRectGetMidY(textRect);
CGFloat maxy = CGRectGetMaxY(textRect);
CGContextMoveToPoint(context, minx, midy);
CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0);
[loc.name drawInRect:CGRectMake(loc.point.x - textSize.width/2.0 + 1, loc.point.y + 21, textSize.width, textSize.height) withFont:[UIFont fontWithName:@"Helvetica" size:14]];
}
}
AREALoctions is a class wich contains the specific position of the "Point of interest"
on the screen and some more informations about the name, etc.
The Problem is, that the UIView's [setNeedsDisplay]
must be called every 1/50.0 seconds (I use a NSTimer to do this) in order to display the points while moving the device. Especially when the size of the array is growing, drawRect:
is very slow and so it comes to lags.
Do you have any ideas to increase the performance of drawRect:
? Maybe prefer CALayer over UIView?
Thank you very much!