gpt4 book ai didi

xcode - UIScrollView 内存分配和释放

转载 作者:行者123 更新时间:2023-12-04 05:38:34 27 4
gpt4 key购买 nike

我正在开发一个非常简单明了的应用程序,它使用几个带有 ScrollView 的页面。我在 ViewDidLoad 中有一个图像数组,然后是一个显示当前页面正负 1 的可变数组。有一个清除页面方法应该从 super View 中删除额外的图像。在具有大量图像的实际设备上运行此程序时,滚动浏览 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

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

谢谢!

最佳答案

将实际图像加载到数组中会产生比仅将名称存储在数组中并加载图像的开销大得多的开销

UIImage *image = [[UIImage alloc] initWithContentsOfFile:[self.pageImages objectAtIndex:page]];
UIImageView *newPageView = [[UIImageView alloc] initWithImage:image];
[image release];

此时,只有 newPageView 会持有内存中图像的引用,当 newPageView 从它的 superview 中移除时,内存中的图像将被释放。

关于xcode - UIScrollView 内存分配和释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11594432/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com