我正在做一个像相册一样的iPad应用程序,我使用带分页的UIScrollView来显示照片图像。我的问题是应用程序在模拟器中运行完美,但在查看大约50张照片后会崩溃。我在泄漏测试中测试它,内存不断增加,我不知道在哪里,以及哪个项目需要释放。我还将加载图像编码更改为“imageWithContentsOfFile”以避免在缓存上加载图像。
我的应用程序包括3个类:AppDelegate,viewController和DetailViewController。 viewController的nib有一个UIScrollView,DetailViewController nid包含UIImageView。
//viewController.m
static NSUInteger kNumberOfPages = 0;
@interface ScrollViewPagingLandscapeViewController (PrivateMethods)
- (void)loadScrollViewWithPage:(int)page;
- (void)scrollViewDidScroll:(UIScrollView *)sender;
@end
- (void)viewDidLoad {
[super viewDidLoad];
kNumberOfPages = [photosArray count];
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
// a page is the width of the scroll view
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
pageControl.numberOfPages = kNumberOfPages;
pageControl.currentPage = 0;
// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
}
- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= kNumberOfPages) return;
// replace the placeholder if necessary
DetailViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[DetailViewController alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (nil == controller.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
// A possible optimization would be to unload the views+controllers which are no longer visible
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.scrollView = nil;
self.viewControllers = nil;
self.totalpage = nil;
}
- (void)dealloc {
[scrollView release];
[viewControllers release];
[totalpage release];
[super dealloc];
}
// DetailViewController.m
- (id)initWithPageNumber:(int)page {
if (self = [super initWithNibName:@"DetailViewController" bundle:nil]) {
pageNumber = page;
}
return self;
}
- (void)dealloc {
[imageView release];
[super dealloc];
}
- (void)viewDidLoad {
NSBundle *bundle = [NSBundle mainBundle];
NSString *imagePath = [bundle pathForResource:[[photosArray objectAtIndex:pageNumber] objectForKey:@"imageName"] ofType:@"jpg"];
UIImage *tempImage = [UIImage imageWithContentsOfFile:imagePath];
imageView.image= tempImage;
}
这是我第一次提问,请提醒我,如果我错过了什么。 还要感谢回答我的问题。