問題描述
我有一個滾動視圖,可以在滾動頁面時顯示不同的圖像,例如 PhotoScroller.我正在使用ARC.當有人滾動到另一個頁面時,我將當前未顯示的 UIImageView 的圖像屬性設置為 nil,因為(試圖)避免內存崩潰,這種情況仍在發生.然后當用戶滾動到一個新頁面時,該頁面的圖像被設置為 UIImageView 的圖像屬性,以及它之前和之后的頁面(為了流暢查看).頁面的 UIImage 都保存在一個數組中.然而,當我滾動頁面時,內存使用量一直在上升,好像將 UIImageView 的 image
屬性設置為 nil 并沒有從內存中釋放它.我使用 initWithContentsOfFile
來初始化我的 UIImages.我也嘗試使用 imageNamed
和 imageWithContentsOfFile
,但沒有成功.這是我的滾動視圖代碼:
I have a scrollview that shows different images as it's scrolled through the pages, like PhotoScroller. I'm using ARC. When someone scrolls to another page, I set the image property of the UIImageView not being currently show to nil, as (attempting) to avoid memory crashes, which are still happening. Then when the user scrolls to a new page, the image for that page is set as the UIImageView's image property, as well as the page before and after it (for smooth viewing). The UIImage's for the pages are all held in an array. Yet as I scroll through the pages, memory usage keeps going up, as if setting the UIImageView's image
property to nil isn't releasing it from memory. I use initWithContentsOfFile
to initialize my UIImages. I tried with imageNamed
and imageWithContentsOfFile
too, with no luck. Here's my scrollview code:
<代碼>- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
int indexShown = self.scrollView.bounds.origin.x/kScrollObjWidth;
for(NSNumber *index in indexesToRemove)
{
UIImageView *imgViewToRemove = [[self.scrollView subviews] objectAtIndex:[index intValue]];
imgViewToRemove.image = nil;
}
[indexesToRemove removeAllObjects];
UIImageView *imgViewToReplace = [[self.scrollView subviews] objectAtIndex:indexShown];
[imgViewToReplace setImage:[pageUIImagesArr objectAtIndex:indexShown]];
[indexesToRemove addObject:[NSNumber numberWithInt:indexShown]];
if(indexShown != 0 && ![[[self.scrollView subviews] objectAtIndex:indexShown-1] image])
{
imgViewToReplace = [[self.scrollView subviews] objectAtIndex:indexShown-1];
[imgViewToReplace setImage:[pageUIImagesArr objectAtIndex:indexShown-1]];
[indexesToRemove addObject:[NSNumber numberWithInt:indexShown-1]];
}
if(indexShown != kNumImages-1 && ![[[self.scrollView subviews] objectAtIndex:indexShown+1] image])
{
imgViewToReplace = [[self.scrollView subviews] objectAtIndex:indexShown+1];
[imgViewToReplace setImage:[pageUIImagesArr objectAtIndex:indexShown+1]];
[indexesToRemove addObject:[NSNumber numberWithInt:indexShown+1]];
}
currentView = [[self.scrollView subviews] objectAtIndex:indexShown];
//check which view is being shown`
推薦答案
頁面的 UIImage 都保存在一個數組中.
The UIImage's for the pages are all held in an array.
當您將 UIImageView
的屬性設置為 nil
時,UIImage
不會被釋放,因為數組仍然持有一個引用給他們.至于內存增長,可能是正在分配的其他東西.我建議查看 Instrument 的對象分配工具,以追蹤滾動時到底有什么增長.
The UIImage
's are not being deallocated when you set the UIImageView
's property to nil
because the array is still holding a reference to them. As for the memory growth, it may be something else that is being allocated. I'd suggest taking a look with Instrument's object allocation instrument to track down what exactly is growing as you scroll.
這篇關于將 uiimage 設置為 nil 不會使用 ARC 釋放內存的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!