iOS UIImageView 内存问题
我一直在尝试解决由于内存警告而释放 UIImageView 中的图像有关的问题。
这是我的情况:
1)我在我的.H中声明一个UIImageView:
IBOutlet UIImageView *fightStyleImage;
...
@property (nonatomic, retain) UIImageView *fightStyleImage;
2)我在我的.M中合成并释放:
@synthesize fightStyleImage;
...
...
-(void)dealloc{
[fightStyleImage release];
...
}
3)我将fightStyleImage附加到Interface Builder中的UIImageView。
4) 我的应用程序的部分体验允许用户在设备上滑动,循环浏览 5 个不同的图像。我使用以下代码来实现这一点:
// Load next image
UIImage *theImg = [UIImage imageNamed:@"fighter1.png"];
fightStyleImage.image = theImg;
一切都运行得很好,我对性能、体验等感到满意。
但是,当我弹出到另一个视图控制器(或 MFMailComposeViewController 或地址簿选择器)并且我的设备收到内存警告(通过模拟器或在设备上),当我返回到原始视图控制器时,会发生以下情况:iOS 似乎抛弃了我的 UIImageView 图像(我认为是因为它是内存中最昂贵的对象),然后我留下黑屏(即没有图像)。如果我再次开始滑动,图像就会返回并继续正常循环。
我很想听听对此的任何想法,并提前感谢任何人可能拥有的任何解决方案/见解。我特别感兴趣(当然!)恢复我已被记忆抛弃的形象。
-SD
I've been trying to solve a problem to do with the image in my UIImageView being released due to memory warnings.
Here's my situation:
1) I declare a UIImageView in my .H:
IBOutlet UIImageView *fightStyleImage;
...
@property (nonatomic, retain) UIImageView *fightStyleImage;
2) I synthesize and dealloc in my .M:
@synthesize fightStyleImage;
...
...
-(void)dealloc{
[fightStyleImage release];
...
}
3) I attach the fightStyleImage to a UIImageView in Interface Builder.
4) Part of my app's experience allows a user to swipe on the device, to cycle through 5 different images. I am using the following code to achieve that:
// Load next image
UIImage *theImg = [UIImage imageNamed:@"fighter1.png"];
fightStyleImage.image = theImg;
Everything works really well, and I'm happy with the performance, experience, etc.
However, when I pop to another view controller (or to an MFMailComposeViewController, or an Address Book picker) and my device receives a memory warning (either through the simulator, or on the device), the following happens when I return to my original view controller: the iOS seems to jettison my UIImageView image (I assume because it is the most expensive object in memory), and I am left with a black screen (i.e. no image). If I begin to swipe again, the images return and continue to cycle properly.
I'd love to hear any thoughts on this, and thanks in advance for any solutions/insights anyone might have. In particular, I'm interested (of course!) in restoring my image that has been jettisoned from memory.
-SD
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要正确实现
viewDidLoad
和viewDidUnload
来设置和释放您的视图。阅读 内存管理编程指南寻求帮助。我还建议您将 IBOutlet 声明从 ivar 移至该属性,如下所示You need to properly implement the
viewDidLoad
and theviewDidUnload
to setup and release your views. Read the iOS section of the Memory Management Programming Guide for assistance. I also recommend you move your IBOutlet declaration from the ivar to the property as so该问题可能是由于使用
imageNamed:
方法造成的,因为它将图像缓存到内存中,并且仅在发生内存不足警告时才会强制释放。最好这样走:The problem maybe due to using
imageNamed:
method as it caches the images into your memory and will be forced to free only when Low Memory warning occurs. Better go this way: