加载滚动视图的性能不佳

时间:2010-10-26 16:22:25

标签: iphone ipad ios core-graphics

我有一个视图控制器,其视图包含一个由2个按钮(上一个和下一个)控制的分页滚动视图。在此滚动视图中有几个自定义视图,每个视图大小为一页。

加载主视图时,滚动视图设置为包含所有这些子视图。我的应用程序的性能目前在这个领域是不可接受的,因为加载视图大约需要3秒钟。我注意到如果我在自定义视图上注释掉我的drawrect方法,性能会得到显着提升。有人可以看看这个代码,看看我在做什么,这是如此资源饥饿?我真的很擅长核心图形,并怀疑我做的事情显然是错误的。

由于

- (void)drawRect:(CGRect)rect {
    UILabel *questionNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, 200, 50)];
    [questionNumberLabel setBackgroundColor:[UIColor clearColor]];
    [questionNumberLabel setText:[NSString stringWithFormat:@"Question %i", surveyQuestionNumber]];
    [questionNumberLabel setTextAlignment:UITextAlignmentCenter];
    [questionNumberLabel setFont:[UIFont boldSystemFontOfSize:28.0]];
    [questionNumberLabel setTextColor:[UIColor whiteColor]];
    [questionNumberLabel setShadowOffset:CGSizeMake(0, -1.0)];
    [questionNumberLabel setShadowColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5]];
    [self addSubview:questionNumberLabel];
    [questionNumberLabel release];

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGColorRef whiteColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor; 
    CGColorRef lightGrayColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0].CGColor;
    CGColorRef shadowColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.5].CGColor;
    CGColorRef lightGreenColor = [[UIColor colorWithRed:158.0/255.0 green:192.0/255.0 blue:72.0/255.0 alpha:1.0] CGColor];
    CGColorRef darkGreenColor = [[UIColor colorWithRed:102.0/255.0 green:142.0/255.0 blue:66.0/255.0 alpha:1.0] CGColor];
    CGColorRef shadowGreenColor = [[UIColor colorWithRed:71.0/255.0 green:100.0/255.0 blue:66.0/255.0 alpha:1.0] CGColor];

    CGFloat outerMargin = 20.0f;
    CGRect outerRect = CGRectInset(self.bounds, outerMargin, outerMargin);
    CGMutablePathRef outerPath = createRoundedRectForRect(outerRect, 15.0);

    CGContextSaveGState(context);
    CGContextSetFillColorWithColor(context, whiteColor);
    CGContextSetShadowWithColor(context, CGSizeMake(0, 5), 5.0, shadowColor);
    CGContextAddPath(context, outerPath);
    CGContextFillPath(context);
    CGContextRestoreGState(context);

    CGContextSaveGState(context);
    CGContextAddPath(context, outerPath);
    CGContextClip(context);
    drawLinearGradient(context, outerRect, whiteColor, lightGrayColor);
    CGContextRestoreGState(context);

    CGContextSaveGState(context);
    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, whiteColor);
    CGContextAddPath(context, outerPath);
    CGContextStrokePath(context);
    CGContextRestoreGState(context);

    CGRect ribbonyRect = CGRectMake(10, 40, 220, 50);
    CGContextSaveGState(context);
    CGMutablePathRef ribbonPath = CGPathCreateMutable();
    CGPathMoveToPoint(ribbonPath, NULL, ribbonyRect.origin.x, ribbonyRect.origin.y);
    CGPathAddLineToPoint(ribbonPath, NULL, ribbonyRect.origin.x+ribbonyRect.size.width, ribbonyRect.origin.y);
    CGPathAddLineToPoint(ribbonPath, NULL, ribbonyRect.origin.x+ribbonyRect.size.width-20, ribbonyRect.origin.y+((ribbonyRect.size.height)/2));
    CGPathAddLineToPoint(ribbonPath, NULL, ribbonyRect.origin.x+ribbonyRect.size.width, ribbonyRect.origin.y+ribbonyRect.size.height);
    CGPathAddLineToPoint(ribbonPath, NULL, ribbonyRect.origin.x, ribbonyRect.origin.y+ribbonyRect.size.height);
    CGPathCloseSubpath(ribbonPath);


    CGContextSaveGState(context);
    CGContextSetShadowWithColor(context, CGSizeMake(0, 5), 5.0, shadowColor);
    CGContextAddPath(context, ribbonPath);
    CGContextFillPath(context);
    CGContextRestoreGState(context);

    CGContextAddPath(context, ribbonPath);
    CGContextClip(context);
    drawLinearGradient(context, ribbonyRect, lightGreenColor, darkGreenColor);
    CGContextRestoreGState(context);



    CGContextSaveGState(context);
    CGContextAddPath(context, ribbonPath);
    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, lightGreenColor);
    CGContextStrokePath(context);
    CGContextRestoreGState(context);

    CGContextSaveGState(context);
    CGContextAddPath(context, ribbonPath);
    CGContextSetLineWidth(context, 1.5);
    CGContextSetStrokeColorWithColor(context, darkGreenColor);
    CGContextStrokePath(context);
    CGContextRestoreGState(context);


    CGContextMoveToPoint(context, ribbonyRect.origin.x, ribbonyRect.origin.y+ribbonyRect.size.height+1.5);
    CGContextAddLineToPoint(context, 19.0, ribbonyRect.origin.y+ribbonyRect.size.height+1.5);
    CGContextAddLineToPoint(context, 19.0, ribbonyRect.origin.y+ribbonyRect.size.height+12.0);
    CGContextSetFillColorWithColor(context, shadowGreenColor);
    CGContextFillPath(context);

    CFRelease(ribbonPath);
    CFRelease(outerPath);

    questionTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 110.0, 568.0, 100.0)];
    [questionTitleLabel setBackgroundColor:[UIColor clearColor]];
    [questionTitleLabel setText:[currentQuestion questionTitle]];
    [questionTitleLabel setFont:[UIFont systemFontOfSize:30.0]];
    [questionTitleLabel setTextAlignment:UITextAlignmentCenter];
    [questionTitleLabel setTextColor:[UIColor grayColor]];
    [questionTitleLabel setShadowColor:[UIColor whiteColor]];
    [questionTitleLabel setShadowOffset:CGSizeMake(0, -1.0)];
    [questionTitleLabel setLineBreakMode:UILineBreakModeWordWrap];
    [questionTitleLabel setNumberOfLines:0];
    [self addSubview:questionTitleLabel];


    questionHintLabel = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 215.0, 568.0, 30.0)];
    [questionHintLabel setBackgroundColor:[UIColor clearColor]];
    [questionHintLabel setText:[currentQuestion questionHint]];
    [questionHintLabel setFont:[UIFont italicSystemFontOfSize:15.0]];
    [questionHintLabel setTextAlignment:UITextAlignmentCenter];
    [questionHintLabel setTextColor:[UIColor grayColor]];
    [questionHintLabel setShadowColor:[UIColor whiteColor]];
    [questionHintLabel setShadowOffset:CGSizeMake(0, -1.0)];
    [self addSubview:questionHintLabel];
}

我已经了解了我最新的代码。如果有人可以查看它here,我会非常感激

2 个答案:

答案 0 :(得分:4)

有两件事情发生在我身上。

1。)在其他地方实例化您的UILabel - 可能在-viewDidLoad中。并在-viewDidLoad中进行必要的UILabel设置。新建对象需要时间。所以,make questionXxxLabel对象是ivars。

2。)沿着相同的路线,在其他地方创建那些CGColorRefs。它们可以是静态类变量或静态类 - 但它们应该只定义一次。

例如,您可以创建一个colors.h文件,该文件可以包含在可以使用这些颜色的位置。

 /*
 * colors.h
 */

//  use _COLORS_ to insure that colors.h is not included multiple times
//  i.e., ANSI standard way of constructing an inclusion guard.

#ifndef _COLORS_
#define _COLORS_

CGColorRef whiteColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor; 
CGColorRef lightGrayColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0].CGColor;
CGColorRef shadowColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.5].CGColor;
CGColorRef lightGreenColor = [[UIColor colorWithRed:158.0/255.0 green:192.0/255.0 blue:72.0/255.0 alpha:1.0] CGColor];
CGColorRef darkGreenColor = [[UIColor colorWithRed:102.0/255.0 green:142.0/255.0 blue:66.0/255.0 alpha:1.0] CGColor];
CGColorRef shadowGreenColor = [[UIColor colorWithRed:71.0/255.0 green:100.0/255.0 blue:66.0/255.0 alpha:1.0] CGColor];

#endif // _COLORS_

答案 1 :(得分:2)

您是否尝试使用Instruments运行应用程序?也许它可以帮助你。