0

我是新的Objective-C編程,我有典型的內存問題。我必須做一個基於導航控制器的應用程序,並通過幾個視圖(使用推視圖控制器)時,加載100個圖像的動畫。在模擬器運作良好,但在電話不......我打開不同的動畫,然後關閉。我正在使用弧來避免這種情況,但似乎沒有奏效。我也嘗試禁用弧並手動釋放UIImageView,但它甚至很快崩潰。以下是其中一個視圖的示例:使用弧的內存問題

//.h 
@interface Gegant_nou : UIViewController { 

IBOutlet UIImageView *ImageViewGegant; 
} 

@property (nonatomic, strong) UIImageView* ImageViewGegant; 

//.m 

- (void)viewDidLoad { 

    [super viewDidLoad]; 


    UIBarButtonItem *rigthButton = [[UIBarButtonItem alloc] initWithTitle:@"Detalls" style:UIBarButtonItemStyleBordered target:self action:@selector(canviarDetalls)]; 
    self.navigationItem.rightBarButtonItem = rigthButton; 
    [rigthButton release]; 


    ImageViewGegant.animationImages [email protected] 
     [[UIImage imageNamed:@"0001.png"], 
     [UIImage imageNamed:@"0002.png"], 
     . load all the images 
     . 
     [UIImage imageNamed:@"0099.png"], 
     [UIImage imageNamed:@"0100.png"]]; 

    ImageViewGegant.animationDuration = 4; 
    ImageViewGegant.animationRepeatCount = 0; 
    [ImageViewGegant startAnimating]; 
    [self.view addSubview:ImageViewGegant]; 

    self.title = @"Gegant nou"; 

    [ImageViewGegant release]; 

} 

- (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)viewDidUnload{ 
    [super viewDidUnload]; 
    [ImageViewGegant release]; 
} 

任何想法爲什麼會發生?感謝你們對我的幫助!

+0

1.哪一行會拋出異常? 2.在iOS6下不推薦使用'-viewDidUnload',你應該避免使用它(它永遠不會被調用)。 3.爲什麼你不使用循環初始化你的圖像數組? – holex

+0

從使用ImageNamed加載vs initwtihcontentsoffile的內存使用有一些爭議。看到這個職位的一些關於其他選項的更好的信息http://stackoverflow.com/questions/6566827/iphone-ios-how-to-load-a-lot-of-image-in-many-folder-and-show -in-a-table-view –

+0

@holex我應該用什麼來代替viewDidUnload? dealloc的? – joan2404

回答

0

問題解決了!發生的事情是我使用UIImageNamed加載動畫,儘管我使用ARC,但UIImageNamed加載圖像但不釋放它,因此加載所有幀並快速填充內存。現在我沒有這種記憶問題。謝謝大家!

1

如果您提供了一些crashlog或stacktrace以提供有關問題出在哪裏的更多信息,那麼協助您將會更容易。

你完全確定這個問題與內存有關嗎?既然你使用的是ARC,那麼除非你有一部分代碼庫沒有使用ARC,或者你使用了像CoreGraphics等這樣的庫,即使你使用ARC,你仍然需要保留/釋放。

如果你確實有內存問題,說過量銷售,並且你得到了EXC_BAD_ACCESS的崩潰,你可以嘗試按照產品 - >編輯方案 - >診斷程序啓用殭屍程序,然後點擊「啓用殭屍對象」。這將有望提供更多有關造成問題的對象的信息。

在完全不同的主題上,我強烈建議您使用camelcase編寫所有實例變量。閱讀起來令人困惑,大多數開發人員會認爲「ImageViewGegant」是一個類的名稱。

+0

我有一個內存問題,就像你說我得到EXC_BAD_ACCESS [在這裏查看捕獲](https://dl.dropbox.com/u/2720540/Captura%20de%20pantalla%202012-09-29%20a%20les% 2021.14.21.png)。我正在使用coregraphics庫。感謝所有。 – joan2404