2010-08-25 72 views
3

我有一個非常大的圖像IntroViewController,這增加了我的應用程序的內存大約1.5MB。該圖像設置在視圖控制器的NIB中的UIImageView上。我的應用程序如何在內存中顯示圖像?

一旦介紹完成後,我會在IntroViewController上調用release,然後它自己成功調用dealloc並在大型UIImageView上調用release。然而,我在樂器中的記憶似乎沒有再次回落。我可以通過將圖像重命名爲NIB中的UIImageView來測試內存差異,以便找不到任何內容,然後內存下降大約1.5MB。

那麼,爲什麼我不能回憶那些回憶呢?是否有什麼,我想念正在停止UIImageView正確dealloc'd?

感謝,

:-Joe

-------增加:CODE ------

#import <UIKit/UIKit.h> 

@class AppDelegate_iPhone; 

@interface IntroViewController : UIViewController 
{ 
    AppDelegate_iPhone *appDelegate; 

    UIImageView *wheelImageView; 
    UIView *copyOverlayView; 
} 

@property (nonatomic, retain) IBOutlet UIImageView *wheelImageView; 
@property (nonatomic, retain) IBOutlet UIView *copyOverlayView; 

- (void)startAnimation; 

@end 

@implementation IntroViewController 

@synthesize wheelImageView, copyOverlayView; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    appDelegate = (AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate]; 
    [self startAnimation]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [self.wheelImageView removeFromSuperview]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    self.wheelImageView.image = nil; 
    appDelegate = nil; 
} 

- (void)dealloc 
{ 
    [wheelImageView release]; 
    [copyOverlayView release]; 

    [super dealloc]; 
} 

- (void)startAnimation 
{ 
    self.wheelImageView.transform = CGAffineTransformMakeScale(0.5, 0.5); 
    [UIView beginAnimations:@"wheelScale" context:nil]; 
    [UIView setAnimationDuration:2.0]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(wheelScaleDidFinish)]; 
    self.wheelImageView.transform = CGAffineTransformMakeScale(1.0, 1.0); 
    [UIView commitAnimations]; 

    [UIView beginAnimations:@"copyOverlayFade" context:nil]; 
    [UIView setAnimationDuration:1.0]; 
    [UIView setAnimationDelay:1.0]; 
    self.copyOverlayView.alpha = 0; 
    [UIView commitAnimations]; 
} 

- (void)wheelScaleDidFinish 
{ 
    [appDelegate introAnimationFinished]; 
} 

@end 

----編輯

燦任何人都請幫忙?我很難過,這導致我的應用程序崩潰在iPhone上由於高內存:(無法弄清楚如何擺脫愚蠢的形象!Grrrr ...

回答

0

好吧,所以我找到了解決方案,但我非常驚訝!

我不僅需要將UIImageView移出IB並動態創建它,我必須避免使用便捷方法來加載圖像。相反的:

wheelImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wheel-intro.png"]; 
[self.view addSubview:self.wheelImageView]; 
[wheelImageView release]; 

我不得不使用:

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"wheel-intro" ofType:@"png"]; 
wheelImage = [[UIImage alloc] initWithContentsOfFile:imagePath]; 
wheelImageView = [[UIImageView alloc] initWithImage:wheelImage]; 
[wheelImage release]; 
[self.view addSubview:self.wheelImageView]; 
[wheelImageView release]; 

現在,當我在viewDidDisappear處理程序做[wheelImageView removeFromSuperview],一切都得到正確清理。

我發現真正奇怪的是,將圖像放入界面生成器在內存管理中與在XCode中使用便捷方法相同。既不能正常工作!注意自我:停止使用方便的方法...

+1

原因是UIImage imageNamed:和朋友維護一個圖片緩存。在壓力下,系統將釋放內存。 – 2010-08-28 07:05:50

+0

所以雖然有autorelease,但有些對象有自己的緩存?這是一個鮮爲人知的事實,謝謝。但是,你說在壓力下系統會釋放內存,但在實踐中,對於我來說,手機會因內存不足而崩潰,並且泄漏仍會顯示緩存的圖像! – jowie 2010-08-28 12:17:55

1

UIImageView正在釋放,但你確定你沒有在其他地方留下幻影嗎?如果我是你,我會試着追蹤它的保留和發佈。你可以用時髦的調試技巧,像malloc_history這樣的工具,或者簡單地通過make a的UIImageView的子類:

@interface DebugImageView @end 
@implementation DebugImageView 
    - (id) retain { 
    NSLog(@"retained"); 
    return [super retain]; 
    } 

    - (void) release { 
    NSLog(@"release"); 
    [super release]; 
    } 

    - (void) dealloc { 
    NSLog(@"dealloc"); 
    [super dealloc]; 
    } 

只要改變類的UIImageView對象在IB到DebugImageView,你應該是好去你也可以把破發點上的,看看他們是所謂的,或只是讓。請撥打電話backtrace()我所在的地方日誌報表。

如果該對象實際上被刪除,那麼你的內存增加可能並不嚴重,很可能是由於大量可重用緩衝區對象被延遲驅逐或堆碎片。儘管1.5MB似乎有點高,所以我懷疑這是一個過度保留。

+0

謝謝 - 我試過了,我看到很多痕跡!儘管如此,它似乎在調用「dealloc」。它可能是因爲它是UIView動畫的一部分,它保存在圖像上?我是否需要釋放或取消委託UIView動畫序列? – jowie 2010-08-25 23:19:21

+0

PS在泄漏,我可以看到,它仍然掛在 - 我有一個1.39MB的Malloc與負責任的圖書館ImageIO ... – jowie 2010-08-25 23:26:36

+0

這是可以想象的圖像可以複製到一個緩衝區支持CALayer,你保留間接無意中保留了動畫的某些部分,但沒有更多的細節,這很難弄清楚。另外,你使用的是什麼init方法,是否有機會保存用於創建圖像的緩衝區(如NSData)? – 2010-08-26 02:10:47

相關問題