我觀看了有關在UIScrollView上實現平鋪的WWDC 2010 104會議,並試圖通過一些更改來實現它,但我遇到了實際內存問題 - 我將UIImageView作爲子視圖添加到UIScrollView,我甚至嘗試刪除超級添加後立即查看 - 並且我還沒有清理我的真實內存。 可能是autoreleasing有問題嗎?UIScrollView和真實內存
- (void)tilePages
{
[scrollView setContentSize:CGSizeMake(scrollView.bounds.size.width * [images count], scrollView.bounds.size.height)];
CGRect visibleBounds = scrollView.bounds;
int firstNeededPageIndex = floorf(CGRectGetMinX(visibleBounds)/CGRectGetWidth(visibleBounds));
int lastNeededPageIndex = floorf((CGRectGetMaxX(visibleBounds)-1)/CGRectGetWidth(visibleBounds));
firstNeededPageIndex = MAX(firstNeededPageIndex, 0);
lastNeededPageIndex = MIN(lastNeededPageIndex, ([images count] - 1));
for (ImageScrollView *page in visiblePages) {
if (page.index < firstNeededPageIndex || page.index > lastNeededPageIndex) {
[recycledPages addObject:page];
[page removeFromSuperview];
}
}
[visiblePages minusSet:recycledPages];
for (int index = firstNeededPageIndex; index <= lastNeededPageIndex; index++) {
if (![self isDisplayingPageForIndex:index]) {
ImageScrollView *page = [self dequeueRecycledPage];
if (page == nil) {
page = [[[ImageScrollView alloc] init] autorelease];
[page setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
[page setContentMode:UIViewContentModeScaleAspectFill];
}
else {
[page setContentMode:UIViewContentModeScaleAspectFit];
}
}
[self configurePage:page forIndex:index];
[scrollView addSubview:page];
[visiblePages addObject:page];
}
}
}
- (ImageScrollView *)dequeueRecycledPage
{
ImageScrollView *page = [recycledPages anyObject];
if (page) {
[[page retain] autorelease];
[recycledPages removeObject:page];
}
return page;
}
- (BOOL)isDisplayingPageForIndex:(NSUInteger)index
{
BOOL foundPage = NO;
for (ImageScrollView *page in visiblePages) {
if (page.index == index) {
foundPage = YES;
break;
}
}
return foundPage;
}
- (void)configurePage:(ImageScrollView *)page forIndex:(NSUInteger)index
{
page.index = index;
page.frame = [self frameForPageAtIndex:index];
[page displayImage:[self imageAtIndex:index]];
}
- (CGRect)frameForPagingScrollView
{
CGRect frame = scrollView.bounds;
return frame;
}
- (CGRect)frameForPageAtIndex:(NSUInteger)index {
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
CGRect pageFrame = pagingScrollViewFrame;
pageFrame.origin.x = (pagingScrollViewFrame.size.width * index);
return pageFrame;
}
- (UIImage *)imageAtIndex:(NSUInteger)index {
return [images objectAtIndex:index];
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.scrollView = nil;
self.textView = nil;
}
- (void)dealloc
{
[super dealloc];
[scrollView release];
[images release];
[recycledPages release];
[visiblePages release];
}
您應該編輯您的問題以顯示如何(在代碼中)創建視圖並添加它們。另外,你使用的是ARC嗎?是什麼促使你的問題開始?您是否收到內存警告和崩潰(當您嘗試加載大量高分辨率或相當大的圖像時可能會發生這種情況)。 – 2012-01-31 09:50:46
我加了我的代碼。不,我沒有使用ARC。 – 2012-01-31 10:13:30
這很好(除了瘋狂的代碼格式化部分),但我也希望能從你那裏得到更多的解釋:是什麼促使你的問題開始?你是否收到內存警告或崩潰?你在使用ARC嗎? – 2012-01-31 10:15:54