CoreGraphics taking a while to show on a large view - can i get it to repeat pixels?

Posted by Andrew on Stack Overflow See other posts from Stack Overflow or by Andrew
Published on 2012-03-18T17:51:08Z Indexed on 2012/03/18 17:55 UTC
Read the original article Hit count: 237

Filed under:
|
|

This is my coregraphics code:

void drawTopPaperBackground(CGContextRef context, CGRect rect) {

    CGRect paper3 = CGRectMake(10, 14, 300, rect.size.height - 14);
    CGRect paper2 = CGRectMake(13, 12, 294, rect.size.height - 12);
    CGRect paper1 = CGRectMake(16, 10, 288, rect.size.height - 10);

    //Shadow
    CGContextSetShadowWithColor(context, CGSizeMake(0,0), 10, [[UIColor colorWithWhite:0 alpha:0.5]CGColor]);
    CGPathRef path = createRoundedRectForRect(paper3, 0);
    CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);
    CGContextAddPath(context, path);
    CGContextFillPath(context);

    //Layers of paper
    //CGContextSaveGState(context);

    drawPaper(context, paper3);
    drawPaper(context, paper2);
    drawPaper(context, paper1);

    //CGContextRestoreGState(context);

}

void drawPaper(CGContextRef context, CGRect rect) {
    //Shadow
    CGContextSaveGState(context);

    CGContextSetShadowWithColor(context, CGSizeMake(0,0), 1, [[UIColor colorWithWhite:0 alpha:0.5]CGColor]);

    CGPathRef path = createRoundedRectForRect(rect, 0);
    CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);
    CGContextAddPath(context, path);
    CGContextFillPath(context);

    //CGContextRestoreGState(context);

    //Gradient

    //CGContextSaveGState(context);

    CGColorRef startColor = [UIColor colorWithWhite:0.92 alpha:1.0].CGColor;
    CGColorRef endColor = [UIColor colorWithWhite:0.94 alpha:1.0].CGColor;

    CGRect firstHalf = CGRectMake(rect.origin.x,
                                  rect.origin.y, rect.size.width / 2, rect.size.height);
    CGRect secondHalf = CGRectMake(rect.origin.x + (rect.size.width / 2),
                                  rect.origin.y, rect.size.width / 2, rect.size.height);

    drawVerticalGradient(context, firstHalf, startColor, endColor);
    drawVerticalGradient(context, secondHalf, endColor, startColor);

    //CGContextRestoreGState(context);

    //CGContextSaveGState(context);

    CGRect redRect = rectForRectWithInset(rect, -1);
    CGMutablePathRef redPath = createRoundedRectForRect(redRect, 0);


    //CGContextSaveGState(context);
    CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);

    CGContextAddPath(context, path);
    CGContextClip(context);

    CGContextAddPath(context, redPath);
    CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 15.0, [[UIColor colorWithWhite:0 alpha:0.1] CGColor]);
    CGContextStrokePath(context);

    CGContextRestoreGState(context);

}

The view is a UIScrollView, which contains a textview. Every time the user types something and goes onto a new line, I call [self setNeedsDisplay]; and it redraws the code. But when the view starts to get long - around 1000 height, it has very noticeable lag. How can i make this code more efficient? Can i take a line of pixels and make it just repeat that, or stretch it, all the way down?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c