在objective-c中指定一個圖片給image view常見的方法為:
[imgView setImage:[UIImage imageNamed:@"imageName.png"]];
但這樣會讓圖片cache在記憶體內而無法被釋放,
當我移除該imageView後,
圖片仍然佔用著記憶體
因此可以改寫成:
NSError *error; NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"png"] options:NSDataReadingUncached error:&error]; UIImage *image = [UIImage imageWithData:data];
雖然看起來多了一些功夫,
但是在該image view從螢幕上移除後,
可以順利的釋放所有佔用的記憶體。
在一個連續播放大量圖片的case中,
圖片最後有沒有被release掉,記憶體使用量差非常多!