我正在开发一个非常简单直接的应用程序,它使用几个带有滚动视图的页面。我在 ViewDidLoad 中有一个图像数组,然后是一个可变数组,显示当前页面加减 1。有一个清除页面方法应该从超级视图中删除额外的图像。在具有大量图像的实际设备上运行此程序时,滚动浏览 50 左右图像后会出现内存警告,然后应用程序崩溃。我通过仪器运行了这个,发现每次滑动都会显着增加内存。在创建数组时,我使用了 imageNamed 和 imageWithContentsOfFile ,无论哪种方式都接近相同的结果。我意识到这里有多个滚动视图问题,但不知何故我似乎无法解决这个问题。至少可以说令人沮丧。我希望有人能以全新的眼光看待这个问题,并对我的问题有所了解。首先十分感谢。
编辑:忘了提及 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
我真的很感谢你花时间来看看这个,并希望有人可以为我指明正确的方向。
谢谢!