简体   繁体   English

从后台返回后,我的iPhone应用程序崩溃了。 原因:UIImage问题

[英]My iPhone app crashes after returning from background. Cause: UIImage problem

First off, I want to say this site is AWESOME! 首先,我想说这个网站真棒! and it helped me do lots of stuff while creating my iPhone app. 它帮助我在创建iPhone应用程序时做了很多事情。

Now, my problem is: 现在,我的问题是:

When I launch my app, I have a UIImageView that loads an image depending on an if/else statements in 启动应用程序时,我有一个UIImageView,它根据其中的if / else语句加载图像

-(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. 当然,BG是UIImageView,someImage是具有@ property,@ synthesis的iVar。 someImage is initialized with an image from the main bundle in viewDidLoad: 使用viewDidLoad中主包中的图像初始化someImage:

- (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! 我的应用程序运行愉快,根据touchBegan(如上所述)加载图像,但是!

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? 我认为someImage已刷新或损坏?

I don't want to leave it like this because imageNamed method reads from disk every time, which will cause performance problems, i think? 我不想这样保留它,因为imageNamed方法每次都从磁盘读取数据,这会导致性能问题,我认为呢?

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? 1-为什么我的应用程序从backgroud返回后会崩溃2-如何解决此问题?

All your help is appreciated! 感谢您的帮助! Thanks! 谢谢!

I'm guessing the crash is EXC_BAD_ACCESS (but am guessing because you didn't post that information). 我猜想崩溃是EXC_BAD_ACCESS(但是我猜是因为您没有发布该信息)。

If "someImage" is an instance variable, you should synthesize it and use its accessor (self.someImage) so it is retained or copied. 如果“ someImage”是一个实例变量,则应对其进行合成并使用其访问器(self.someImage),以便对其进行保留或复制。 As it stands, you're assigning something to someImage but it's gone by the time you're trying to access it later. 就目前而言,您正在为someImage分配某些内容,但是当您稍后尝试访问它时,它已经消失了。

When you do this: 当你这样做:

- (void)viewDidLoad {

    //init stuff from file
    someImage = [UIImage imageNamed:@"FirstViewBG_5N.png"];

    [super viewDidLoad];
}

The imageNamed method is returning an autoreleased object which gets cleaned up by the garbage collector after viewDidLoad returns. imageNamed方法将返回一个自动释放的对象,该对象将在viewDidLoad返回后由垃圾收集器清除。 Try either retaining it: 尝试保留它:

    someImage = [[UIImage imageNamed:@"FirstViewBG_5N.png"] retain];

or using your synthesized setter which will retain it automatically: 或使用您将自动保留的合成二传手:

    [self setSomeImage:[UIImage imageNamed:@"FirstViewBG_5N.png"]];

or using UIImage's initWithData initializer: 或使用UIImage的initWithData初始化程序:

    someImage = [[UIImage alloc] initWithContentsOfFile:@"FirstViewBG_5N.png"];

All are functionally equivalent. 在功能上都是等效的。 #2 or #3 is best. #2或#3最好。

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 Apple的“内存管理规则”指南将挽救您的生命: http : //developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html%23//apple_ref/doc/uid/ 20000994-BAJHFBGH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM