我的 iPhone 应用程序从后台返回后崩溃。原因:UIImage问题
首先,我想说这个网站太棒了!它帮助我在创建 iPhone 应用程序时完成了很多事情。
现在,我的问题是:
中的 if/else 语句加载图像
当我启动我的应用程序时,我有一个 UIImageView,它根据-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event< /代码>
方法。这些图像的分配如下:
BG.image = someImage;
当然,BG是UIImageView,someImage是带有@property、@synthesis的iVar。 someImage 使用 viewDidLoad 中主包中的图像进行初始化:
- (void)viewDidLoad {
//init stuff from file
someImage = [UIImage imageNamed:@"FirstViewBG_5N.png"];
[super viewDidLoad];}
我的应用程序运行愉快,根据 touchBegan 加载图像(如上所述),但是!
当我的应用程序发送到后台并返回时,它会在第一次触摸时崩溃。
当我将: 替换
BG.image = someImage
为: 时,
BG.image = [UIImage imageNamed:@"FirstViewBG_5N.png"];
它运行得很愉快?!我认为 someImage 已被刷新或损坏?
我不想这样,因为 imageNamed 方法每次都会从磁盘读取,我认为这会导致性能问题?
我想我的问题很清楚了?就是这样:
1-为什么我的应用程序从后台返回后会崩溃 2-我该如何解决这个问题?
感谢您的所有帮助! 谢谢!
First off, I want to say this site is AWESOME! and it helped me do lots of stuff while creating my iPhone app.
Now, my problem is:
When I launch my app, I have a UIImageView that loads an image depending on an if/else statements in
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
method. These images are assigned as follows:
BG.image = someImage;
of course, BG is the UIImageView, and someImage is an iVar with @property, @synthesis. someImage is initialized with an image from the main bundle in viewDidLoad:
- (void)viewDidLoad {
//init stuff from file
someImage = [UIImage imageNamed:@"FirstViewBG_5N.png"];
[super viewDidLoad];}
My app runs happily, loading images according to touchBegan (as mentioned), BUT!
When my app is sent to background and comes back, it crashes upon first touch.
When I replaced:
BG.image = someImage
with:
BG.image = [UIImage imageNamed:@"FirstViewBG_5N.png"];
it runs happily?! I think the someImage is flushed or corrupts?
I don't want to leave it like this because imageNamed method reads from disk every time, which will cause performance problems, i think?
I think my question is clear? It is that:
1- Why will my app crash after returning from backgroud
2- How do I solve this?
All your help is appreciated!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜测崩溃是 EXC_BAD_ACCESS (但我猜测是因为您没有发布该信息)。
如果“someImage”是一个实例变量,您应该合成它并使用它的访问器(self.someImage),以便保留或复制它。就目前情况而言,您正在为 someImage 分配一些内容,但当您稍后尝试访问它时,它已经消失了。
I'm guessing the crash is EXC_BAD_ACCESS (but am guessing because you didn't post that information).
If "someImage" is an instance variable, you should synthesize it and use its accessor (self.someImage) so it is retained or copied. As it stands, you're assigning something to someImage but it's gone by the time you're trying to access it later.
当您执行此操作时:
imageNamed
方法将返回一个自动释放的对象,该对象在viewDidLoad
返回后由垃圾收集器清理。尝试保留它:或使用将自动保留它的合成设置器:
或使用 UIImage 的
initWithData
初始值设定项:所有功能在功能上都是等效的。 #2 或 #3 最好。
Apple 的“内存管理规则”指南将拯救您的生命:http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html%23//apple_ref/文档/uid/20000994-BAJHFBGH
When you do this:
The
imageNamed
method is returning an autoreleased object which gets cleaned up by the garbage collector afterviewDidLoad
returns. Try either retaining it:or using your synthesized setter which will retain it automatically:
or using UIImage's
initWithData
initializer:All are functionally equivalent. #2 or #3 is best.
Apple's "Memory Management Rules" guide will save your life: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html%23//apple_ref/doc/uid/20000994-BAJHFBGH