UIScrollView内存分配和发布

时间:2012-07-21 17:37:54

标签: xcode memory-management uiscrollview

我正在开发一个非常简单直接的应用程序,该应用程序使用带滚动视图的几页。我在ViewDidLoad中有一个图像数组,然后是一个显示当前页面加减1的可变数组。有一个清除页面方法可以从superview中删除多余的图像。在具有大量图像的实际设备上运行此操作时,在滚动50个左右的图像然后应用程序崩溃后会出现内存警告。我通过仪器运行它,看到每次刷卡时内存都在大幅增加。在创建数组时,我使用了imageNamed以及imageWithContentsOfFile,并且两种方式都给出了接近相同的结果。我知道这里有多个scrollview问题,但不知怎的,我似乎无法超越这个问题。至少可以说是沮丧。我希望有人可以用一双新鲜的眼睛来看待这个问题并对我的问题有所了解。非常感谢提前。

编辑:忘记提及使用ARC。是的,我使用ARC。

代码:.h文件

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController <UIScrollViewDelegate>

@property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) IBOutlet UIPageControl *pageControl;

@end

和.m文件

#import "FirstViewController.h"

@interface FirstViewController ()

@property (nonatomic, strong) NSArray *pageImages;
@property (nonatomic, strong) NSMutableArray *pageViews;

- (void)loadVisiblePages;
- (void)loadPage:(NSInteger)page;
- (void)purgePage:(NSInteger)page;
@end

@implementation FirstViewController


@synthesize scrollView = _scrollView;
@synthesize pageControl = _pageControl;

@synthesize pageImages = _pageImages;
@synthesize pageViews = _pageViews;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)loadVisiblePages {
    // First, determine which page is currently visible
    CGFloat pageWidth = self.scrollView.frame.size.width;
    NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth)     / (pageWidth * 2.0f));

// Update the page control
self.pageControl.currentPage = page;

// Work out which pages you want to load
NSInteger firstPage = page - 1;
NSInteger lastPage = page + 1;

// Purge anything before the first page
for (NSInteger i=0; i<firstPage; i++) {
    [self purgePage:i];
}

// Load pages in our range
for (NSInteger i=firstPage; i<=lastPage; i++) {
    [self loadPage:i];
}

// Purge anything after the last page
for (NSInteger i=lastPage+1; i<self.pageImages.count; i++) {
    [self purgePage:i];
}

}

- (void)purgePage:(NSInteger)page {
    if (page < 0 || page >= self.pageImages.count) {
    // If it's outside the range of what you have to display, then do nothing
    return;
    }

    // Remove a page from the scroll view and reset the container array
    UIView *pageView = [self.pageViews objectAtIndex:page];
    if ((NSNull*)pageView != [NSNull null]) {
        [pageView removeFromSuperview];
        [self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];


    }

}


- (void)loadPage:(NSInteger)page {
    if (page < 0 || page >= self.pageImages.count) {
        // If it's outside the range of what you have to display, then do nothing
        return;
    }

    // 1
    UIView *pageView = [self.pageViews objectAtIndex:page];
    if ((NSNull*)pageView == [NSNull null]) {
        // 2
        CGRect frame = self.scrollView.bounds;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0.0f;

        // 3
        UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]];
        newPageView.contentMode = UIViewContentModeScaleAspectFit;
        newPageView.frame = frame;
        [self.scrollView addSubview:newPageView];
        // 4
       [self.pageViews replaceObjectAtIndex:page withObject:newPageView];

    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // Load the pages that are now on screen
    [self loadVisiblePages];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // 1
    self.pageImages = [NSArray arrayWithObjects:
                       [UIImage imageNamed:@"BBD1.jpg"],
                       [UIImage imageNamed:@"BBD2.jpg"],
                       [UIImage imageNamed:@"BBD3.jpg"],
                       [UIImage imageNamed:@"BBD4.jpg"],
                       [UIImage imageNamed:@"BBD5.jpg"],
                       [UIImage imageNamed:@"BBD6.jpg"],
                       [UIImage imageNamed:@"BBD7.jpg"],
                  .....
                       [UIImage imageNamed:@"BBD292.jpg"],
                       [UIImage imageNamed:@"BBD293.jpg"],
                       [UIImage imageNamed:@"BBD294.jpg"],
                       [UIImage imageNamed:@"BBD295.jpg"],
                       [UIImage imageNamed:@"BBD296.jpg"],
                       [UIImage imageNamed:@"BBD297.jpg"],
                       [UIImage imageNamed:@"BBD298.jpg"],
                       [UIImage imageNamed:@"BBD299.jpg"],
                       [UIImage imageNamed:@"BBD300.jpg"],                       


                       nil];

    NSInteger pageCount = self.pageImages.count;

    // 2
    self.pageControl.currentPage = 0;
    self.pageControl.numberOfPages = pageCount;

    // 3
    self.pageViews = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < pageCount; ++i) {
        [self.pageViews addObject:[NSNull null]];
    } 
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // 4
    CGSize pagesScrollViewSize = self.scrollView.frame.size;
    self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width *     self.pageImages.count, pagesScrollViewSize.height);

    // 5
    [self loadVisiblePages];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:    (UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

@end

我真的很感谢你花时间看这个并希望有人能指出我正确的方向。

谢谢!

2 个答案:

答案 0 :(得分:3)

将实际图像加载到数组中会产生比仅在数组中存储名称并加载图像(例如

)更大的开销
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[self.pageImages objectAtIndex:page]];
UIImageView *newPageView = [[UIImageView alloc] initWithImage:image];
[image release];

此时,只有newPageView将在内存中保存对图像的引用,并且当从其超级视图中删除该newPageView时,内存中的图像将被释放。

答案 1 :(得分:0)

您没有说是否使用ARC,但没有ARC,内存管理存在明显问题。在评论“// 3”的loadPage:中,您创建保留计数为1的newPageView,然后将其添加为self.scrollView的子视图并添加到数组self.pageViews。因此它的总保留数为+3。在purgePage:中,您将其从self.scrollViewself.pageViews中删除,但仍保留+1总保留计数。因此,每个页面的UIImageView都会被遗忘,但不会被取消分配。

您可以通过在项目中使用ARC或修改创建newPageView的行来解决此问题:

UIImageView *newPageView = [[[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]] autorelease];