向viewcontrollers默认视图添加许多子视图会使我的应用程序变慢吗?

[英]Does adding many many subviews to a viewcontrollers default view make my app slow?


I have an app where I create many uiviews and add them to the self.view of the UIViewController. My app is running really slowly. I am releasing all of my objects and have no memory leaks (I ran the performance tool). Can anyone tell me what could be making my app so slow? (code is below)

我有一个应用程序,我创建了许多uiviews并将它们添加到UIViewController的self.view。我的应用运行速度非常慢。我正在释放所有对象并且没有内存泄漏(我运行了性能工具)。谁能告诉我什么可能让我的应用程序这么慢? (代码如下)

[EDIT] The array has around 30 items. [/EndEdit]

[编辑]阵列有大约30项。 [/ EndEdit中]

Thanks so much!

非常感谢!

Here is the code for the loadView method of my UIViewController:

这是我的UIViewController的loadView方法的代码:

- (void)loadView {

    UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    contentView.backgroundColor = [UIColor whiteColor];
    self.view = contentView;
    [contentView release]; 



    int length = 0;
    for(NSString *item in arrayTips)
    {
        length++;
        [item release];
    }
    int index = 0;
    for(NSString *item in arrayTitles)
    {
        SingleFlipView *backView = [[SingleFlipView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];



        backView.userInteractionEnabled = YES;
        backView.backgroundColor = [UIColor whiteColor];
        [backView setViewIndex:index];
        [backView setLastViewIndex:length];
        CGRect labelFrame = CGRectMake(10.0f, 0.0f, 300.0f, 30.0f);
        UILabel *backLabel = [[UILabel alloc] initWithFrame:labelFrame];
        backLabel.textAlignment = UITextAlignmentCenter;
        backLabel.userInteractionEnabled = YES;
        backLabel.text = item;
        backLabel.font = [UIFont fontWithName:@"Georgia" size:24.0f];
        backLabel.textColor = [UIColor blackColor];
        backLabel.backgroundColor = [UIColor whiteColor];

        CGRect textFrame = CGRectMake(10.0f, 30.0f, 300.0f, 110.0f);
        UITextView *tbxView = [[UITextView alloc] initWithFrame:textFrame];
        tbxView.textAlignment = UITextAlignmentCenter;
        tbxView.userInteractionEnabled = YES;
        tbxView.editable = FALSE;
        tbxView.text = [arrayTips objectAtIndex:index];
        tbxView.font = [UIFont fontWithName:@"Arial" size:14.0f];
        tbxView.textColor = [UIColor blackColor];
        tbxView.backgroundColor = [UIColor whiteColor];

        //CGRect labelFrame = CGRectMake(10.0f, 0.0f, 84.0f, 30.0f);
        UIImage *nextTip = [[UIImage imageNamed:@"NextTip.png"] retain];
        UIImageView *nextTipView = [ [ UIImageView alloc ] initWithImage:nextTip];
        nextTipView.frame = CGRectMake(230.0f, -10.0f, 84.0f, 30.0f);
        nextTipView.userInteractionEnabled = YES;


        UIImageView *view = [[ UIImageView alloc ] init];
        view.userInteractionEnabled = YES;
        if(self.sexString == @"Men")
        {
            UIImage *imgTip = [[UIImage imageNamed:@"feet_small.jpg"] retain];
            view.image = imgTip;
            view.frame = CGRectMake(0.0f, 110.0f, 416.0f, 228.0f); //59*161
            [imgTip release];
        }

        [backView addSubview:view];
        [backView addSubview:tbxView];
        [backView addSubview:backLabel];
        //[backView addSubview:nextTipView];

        [self.view addSubview:backView];
        [backView release];
        [backLabel release];
        [nextTip release];
        [nextTipView release];
        [tbxView release];
        [view release];

        index++;

        [item release];
    }


}

3 个解决方案

#1


3  

Having deep view hierarchies can lead to slow downs that you can often fix through flattening them some with custom views, but if you are using simple views you can have dozens on the screen with no perceptible performance impact, so in general I recommend ignoring how many views you have when you are developing, and then reducing the view count if it proves to be a performance problem.

拥有深层视图层次结构可能会导致速度变慢,您可以通过使用自定义视图对其进行扁平处理来解决这些问题,但如果您使用简单视图,则屏幕上可能会有数十个视图,而且没有明显的性能影响,所以一般情况下我建议忽略多少您在开发时拥有的视图,如果它被证明是性能问题,则会减少视图数。

Having said that, you appear to be setting up something with an unboundedily large number of views which is not good. Without knowing how many entries there are in array titles I can't tell you what is going on exactly, but I suspect that while the actual visual heiarchy with each backView you are creating is fine, making a backView for each item in the array and using indices to have the front most one hide all the other ones behind it is causing you to have way too many views.

话虽如此,你似乎正在建立一些具有无限大量观点的东西,这是不好的。在不知道数组标题中有多少条目的情况下,我无法告诉你究竟发生了什么,但我怀疑,虽然你正在创建的每个backView的实际可视化heiarchy都很好,但是为数组中的每个项目创建一个backView使用索引使最前面的一个隐藏其后面的所有其他导致您拥有太多的视图。

So, how to test it:

那么,如何测试它:

Add a break to the bottom of your for loop. Make the loop drop out after a single iteration and see if performance improves. If it does, then the huge view hierarchies are your issue. YOu may have to hack up the routine that changes the indexes to make sure it never swaps to an invalid index to test.

在for循环的底部添加一个中断。在单次迭代后使循环退出并查看性能是否有所改善。如果是这样,那么巨大的视图层次结构就是你的问题。你可能不得不破解改变索引的例程,以确保它永远不会交换到无效的索引进行测试。

If that is the case you have a few options. You could implement a custom view and flatten every backview into a single view, but depending on how many you have that mat not be sufficient, and it is more work than simply building the back views the way you currently are, but on demand instead of at load time:

如果是这种情况,您有几个选择。您可以实现一个自定义视图并将每个后视图展平为一个视图,但是根据您拥有的数量不足,并且只需按照您当前的方式构建后视图,而不是按需而不是在加载时:

1) Refactor the code in your for loop into a separate method that makes a backView for a specific title and attaches it to the view at that time. 2) Where ever you are currently altering the indexes to make the backview visible, instead call the new method to actually build and attach the backview at that time

1)将for循环中的代码重构为一个单独的方法,该方法为特定标题创建一个backView,并将其附加到视图中。 2)你当前正在改变索引以使后视图可见,而是调用新方法来实际构建并附加当时的后视图

#2


4  

It's going to depend upon how many items are in arrayTitles. If you're just adding one or two of these, you shouldn't see a HUGE slowdown; more, and you will. You should probably take a look at the way UITableView handles its cells; only create these as they're actually needed/used, or, better yet, only create one of these, and set its contents on-the-fly.

它将取决于arrayTitles中有多少项。如果你只是添加其中的一个或两个,你不应该看到巨大的减速;更多,你会的。您应该看看UITableView处理其单元格的方式;只创建它们,因为它们实际上是需要/使用的,或者更好的是,只创建其中一个,并即时设置其内容。

A few other notes:

其他几点说明:

  • == is not a valid string comparison operator in Objective-C; use [string1 isEqualTo: string2]
  • ==不是Objective-C中有效的字符串比较运算符;使用[string1 isEqualTo:string2]

  • It appears you're trying to place a lot of these on screen at the same time, which doesn't seem like it would make a lot of sense.
  • 看起来你正试图在屏幕上同时放置很多这些,这似乎没有多大意义。

  • it looks like you've got a spurious [item release] at the end there (you're never retaining item, so there's no need to release it.
  • 看起来你在那里有一个虚假的[物品释放](你永远不会保留物品,所以没有必要释放它。

  • the whole first loop ( for(NSString *item in arrayTips)... frightens and confuses me; items in NSArrays are already retained by the array. You shouldn't have to explicitly retain/release them in this way.
  • 整个第一个循环(for(NSTtring * item in arrayTips)...吓坏我并混淆我; NSArrays中的项已经被数组保留了。你不应该以这种方式显式保留/释放它们。

#3


1  

Don't forget to make as many of your views opaque as you can. Transparent views are a major source of performance issues.

不要忘记让尽可能多的视图不透明。透明视图是性能问题的主要来源。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2008/10/31/534df97fdca08a37379c8244af9c20.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告