ios - 删除或重置返回 root 时使用的内存 viewcontroller

ios - Delete or reset memory used when going back to root viewcontroller

美好的一天!有什么方法可以减少或恢复使用的内存到新打开的应用程序状态?我正在为餐厅开发菜单应用程序。在主屏幕上,我有一个用于主菜、开胃菜、汤等的按钮。如果您单击一个类别,它将带您到另一个视图,即带有 UITableViewController 的 UINavigation Controller,现在当您单击任何行时该 UITableView 将带您到另一个 ViewController,其中包含带图像的 UIScrollView。这 Last ViewController 有这段代码可以循环许多图像。

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
                                                                     self.view.frame.size.width,
                                                                     self.view.frame.size.height)];
    self.scrollView.pagingEnabled = YES;
    [self.scrollView setAlwaysBounceHorizontal:NO];
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.scrollEnabled = YES;

    //setup internal views
    NSInteger numberOfViews = 5;
    for (int i = 0; i < numberOfViews; i++) {
        CGFloat xOrigin = i * self.view.frame.size.width;
        image = [[UIImageView alloc] initWithFrame:
                              CGRectMake(xOrigin, 0,
                                         self.view.frame.size.width,
                                         self.view.frame.size.height)];
        image.image = [UIImage imageNamed:[NSString stringWithFormat:
                                           @"main_%d", i+1]];
        image.contentMode = UIViewContentModeScaleAspectFit;

        [self.scrollView addSubview:image];

        [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updatePrices:) userInfo:nil repeats:YES];


    }

    //set the scroll view content size
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width *
                                             numberOfViews,
                                             self.view.frame.size.height);

    //add the scrollview to this view
    [self.view addSubview:self.scrollView];

所以基本上在 1 个类别让我们说主菜,我有这个

http://oi60.tinypic.com/f1fr0j.jpg

每个类别我都有该视图设置。现在,主要问题是,当我打开应用程序时,它只消耗 6MB,然后当我点击一个按钮以显示 selected 类别的菜单时,它上升到 17mb,现在当我点击任何东西时在食物列表中,并显示图像 viewcontroller,它上升到 68mb。我尝试在每个按钮上添加一个按钮并添加一些像这样的操作

- (IBAction)backToHome:(id)sender {

    [self dismissViewControllerAnimated:YES completion:nil];
    self.view = nil;
}

主要问题是,当我回到 MainViewController 时,消耗的内存就在那里,所以如果 select 更多类别,应用程序会变得非常慢,因为它会增加内存使用.如何减少或 "Clear Cache" 已用内存以防止应用程序变慢?谢谢!

调用 imageNamed 将始终将图像缓存在内存中以提高速度,因此即使您释放持有者视图,内存使用量也不会下降。使用其他方法加载图像(imageWithContentsOfFile 或其他)并在需要时自行缓存。

@property(nonatomic, strong) NSMutableDictionary* cachedImages;

-(UIImage*)imageNamedBOOM:(NSString*)aImageName
{
    UIImage* result = _cachedImages[aImageName];
    if( !result )
    {
        result = [UIImage imageWithContentsOfFile:aImageName];
        _cachedImages[aImageName] = result;
    }

    return result;
}

-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

    [_cachedImages removeAllObjects];
}

然后在您的代码中这样调用它(请注意,您必须提供图像扩展名 png、jpg 等):

-(void) setupScrollView {
    //add the scrollview to the view
   self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 
                                           self.view.frame.size.width, 
                                           self.view.frame.size.height)];
   self.scrollView.pagingEnabled = YES;
   [self.scrollView setAlwaysBounceVertical:NO];
    //setup internal views
   NSInteger numberOfViews = 3;
   for (int i = 0; i < numberOfViews; i++) {
      CGFloat xOrigin = i * self.view.frame.size.width;
      UIImageView *image = [[UIImageView alloc] initWithFrame:
                                                CGRectMake(xOrigin, 0, 
                                                self.view.frame.size.width, 
                                                self.view.frame.size.height)];
      image.image = [self imageNamedBOOM:[NSString stringWithFormat:
                                        @"image_%d.png", i+1]];//make sure here you provide proper image extension JPG, PNG or other
      image.contentMode = UIViewContentModeScaleAspectFit;
      [self.scrollView addSubview:image];
    }
    //set the scroll view content size
   self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 
                                            numberOfViews, 
                                            self.view.frame.size.height);
    //add the scrollview to this view
   [self.view addSubview:self.scrollView];
}

回到家:

- (IBAction)backToHome:(id)sender {

    [_cachedImages removeAllObjects]

    [self dismissViewControllerAnimated:YES completion:nil];
    self.view = nil;
}