Objective c 优化建议-drawRect:method
我想画31个不同笔划宽度的圆圈。在我看来,我不能重复使用它,因为我不能扩展它,所有的圈子都有不同的选择。这些视图不是静态的,当我按下视图控制器时,我总是必须重新绘制它 更新部分: 下面是我在CircleView.m中的drawRect:method的时间分析器屏幕截图,当时我转换为查看31个圆圈: 按照这个计划,用户会经常交换视图,每次等待重画都会变得很烦人。 我有几个自定义容器视图。另外约30%的时间用于从故事板加载视图。然而,这对我来说似乎很奇怪,因为现在theOtherTimeWasteView只有一个子视图:DatePickerView,但根据TProfile,运行时间是84ms,100%花在[super loadView]方法上 我天真的优化尝试: 我以前曾尝试加载此视图一次,将其存储在parentViewController combinedViewController中,并一直重复使用它。我在CircleView.h中添加了属性: 然后我通过添加一个call-createImage方法对CircleView.m进行了一些更改:Objective c 优化建议-drawRect:method,objective-c,core-graphics,drawrect,cgcontext,Objective C,Core Graphics,Drawrect,Cgcontext,我想画31个不同笔划宽度的圆圈。在我看来,我不能重复使用它,因为我不能扩展它,所有的圈子都有不同的选择。这些视图不是静态的,当我按下视图控制器时,我总是必须重新绘制它 更新部分: 下面是我在CircleView.m中的drawRect:method的时间分析器屏幕截图,当时我转换为查看31个圆圈: 按照这个计划,用户会经常交换视图,每次等待重画都会变得很烦人。 我有几个自定义容器视图。另外约30%的时间用于从故事板加载视图。然而,这对我来说似乎很奇怪,因为现在theOtherTimeWast
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.imageCircle=[self createImage];
}
return self;
}
-(UIImage*)createImage
{
CGSize size=self.frame.size;
UIGraphicsBeginImageContextWithOptions(size, NO, 1.0);
CGContextRef contextRef = UIGraphicsGetCurrentContext();
self.brush=0.6;
CGRect frameOfCircle=CGRectMake(self.brush/2,self.brush/2, self.frame.size.width-self.brush-1.0f, self.frame.size.height-self.brush);
// Draw a circle (border only)
CGContextSetLineWidth(contextRef, self.brush);
CGFloat mainColor[4]={23.0f/255.0f, 12.0f/255.0f, 0.0f, 0.5};
CGContextSetStrokeColor(contextRef, mainColor);
CGContextStrokeEllipseInRect(contextRef, frameOfCircle);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
combinedViewController.m:
以下是我尝试在可见区域外初始化前10个圆的步骤:
-(void)createCircles
{
self.arrayOfCircles=[[NSMutableArray alloc]init];
for (int i=0; i<10; i++)
{
[self.arrayOfCircles addObject:[self circleViewWithRadius:530+i*35 withBrush:5+i tag:1014+i alphaParam:0.60]];
}
}
我想在CircleViewController.m中添加子视图:
a不幸的是,如果我尝试第二种方法,就不会添加任何圆圈。我调试了断点,arrayOfCircles不为零,circle的image属性也不为零。然而,在我看来,仍然没有圆圈。我的密码有错误吗
b通过提前从CGContext获取图像来优化性能是否合适?如果不是,我恳请您忽略我的“a”问题,并给出一些如何获得高绩效的建议。
非常感谢
更新2:
链接:
当您使用时间分析器模板在Instruments中分析动画时,您发现了什么?@PeterHosey,请注意我更新了我的问题。这是一个高度缩写的屏幕截图,几乎只显示了已经很明显的内容。您应该能够在相同的大纲中找到更多信息,特别是在drawRect的后代中:它调用的方法和函数,以及它们调用的方法和函数,等等。。此外,不要忽略其他顶级项目;您将近40%的时间都花在了其他地方。@PeterHosey,很抱歉,我认为这是不必要的信息,因为我有非常简单的drawRect:method,它调用了一些方法,这些方法在一开始就给出了。然而,CGContextStrokeEllipseInRectcontextRef,frameOfCircle;这种方法花费的时间太多。请参阅更新。根据我的经验,最好对Instruments的带注释的代码视图持保留态度。请改为在大纲视图中显示子代行。
-(void)createCircles
{
self.arrayOfCircles=[[NSMutableArray alloc]init];
for (int i=0; i<10; i++)
{
[self.arrayOfCircles addObject:[self circleViewWithRadius:530+i*35 withBrush:5+i tag:1014+i alphaParam:0.60]];
}
}
-(void)addCircles
{
CombinedViewController *parentVC=(CombinedViewController*)[self parentViewController];
for (int i=0; i<10; i++)
{
CircleView *circle=(CircleView*)[parentVC.arrayOfCircles objectAtIndex:i];
UIImageView *imgv=[[UIImageView alloc]initWithImage:circle.imageCircle];
imgv.center=CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2);
[self.view insertSubview:imgv belowView:someView];
}
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self addCircles];
}