0

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!

Prasad G
  • 6,702
  • 7
  • 42
  • 65
Philip
  • 112
  • 14

1 Answers1

1

Can't tell exactly what you are drawing there - but yeah, calling drawRect that frequently is going to cause major lag. Create a custom UIView for each element 'whole' and then just move them around/updating text only if needed, etc.

Depends totally on the nature of what you are drawing, of course.

spring
  • 18,009
  • 15
  • 80
  • 160
  • the thing is: i cant move them around. each time in my update timer start calling a methode, i have to calculate the distance and heading of surrounding points. Out of this i calculate the position in the view. since my display has a size of 320x480 i also have to check if the point is visible. – Philip Jul 06 '12 at 10:57
  • the best thing would be a '360-degree-view'. I could draw all points on that view while starting the app and then just move around the view from left to right. but i have absolutly no clue how to do this :D – Philip Jul 06 '12 at 11:00
  • Hard to say without seeing it + the details (how many elements, etc.). If these are just sort of label blocks then I would think a custom UIView class would work - and you could optimize a la tableView: have an array (queue) so you don't have to keep creating + destroying them. – spring Jul 06 '12 at 11:04