收到内存警告。在画廊部分Xcode

时间:2014-02-06 19:24:31

标签: ios objective-c xcode uiscrollview

我正在我的应用程序中制作一个手册表格单元格,我有7个页面,每个页面都是图像,我正在使用带有页面控制的uiscrollview,但是当我运行应用程序时,它会显示已接收内存警告。  然后应用程序冻结并自动关闭,我该怎么办,这是我的代码。

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

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

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

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

    #pragma mark -

    - (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 we 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];
}
for (NSInteger i=firstPage; i<=lastPage; i++) {
    [self loadPage:i];
}
for (NSInteger i=lastPage+1; i<self.pageImages.count; i++) {
    [self purgePage:i];
}
    }

    - (UIImage *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;

    }



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

// Load an individual page, first seeing if we've already loaded it
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView == [NSNull null]) {
    CGRect frame = self.scrollView.bounds;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0.0f;

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

    - (void)purgePage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
    // If it's outside the range of what we 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]];
}
    }


    #pragma mark -

    - (void)viewDidLoad {
[super viewDidLoad];

self.title = @"Student Handbook";

// Set up the image we want to scroll & zoom and add it to the scroll view
self.pageImages = [NSArray arrayWithObjects:
                   [UIImage imageNamed:@"page 1 handbook.png"],
                   [UIImage imageNamed:@"page 2 handbook.png"],
                   [UIImage imageNamed:@"page 3 hand book.png"],
                   [UIImage imageNamed:@"page 4 handbook.png"],
                   [UIImage imageNamed:@"page 5 handbook.png"],
                   [UIImage imageNamed:@"page 6 handbook.png"],
                   [UIImage imageNamed:@"page 7 handbook.png"],
                   nil];

NSInteger pageCount = self.pageImages.count;

// Set up the page control
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageCount;

// Set up the array to hold the views for each page
self.pageViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < pageCount; ++i) {
    [self.pageViews addObject:[NSNull null]];
}
    }

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

// Set up the content size of the scroll view
CGSize pagesScrollViewSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count,         pagesScrollViewSize.height);

// Load the initial set of pages that are on screen
[self loadVisiblePages];
    }

    - (void)viewDidUnload {
[super viewDidUnload];

self.scrollView = nil;
self.pageControl = nil;
self.pageImages = nil;
self.pageViews = nil;
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation                                 {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }


    #pragma mark - UIScrollViewDelegate

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

    @end

1 个答案:

答案 0 :(得分:0)

首先确保您没有其他应用正在运行(双击主页按钮并轻弹所有正在运行的应用程序以关闭)。可能图像很大,设备内存不足。如果您能够在尝试确定您是否没有内存泄漏后运行您的应用程序。滑动几次并跟随你的记忆。

enter image description here

如果你使用ARC而不是为你的vars指定nil,因为你不打算使用它们。