views:

10

answers:

2

Hi all,

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!

+1  A: 

'someImage' got released. retain it

GameBit
A: 
  1. Probably because the image was released. The imageNamed method in UIImage class does not return a +1 reference count for your someImage variable so you don't own it.
  2. Assuming the someImage is a property with retain do: self.someImage = [UIImage imageNamed:@"FirstViewBG_5N.png"];

p.s. be careful to release it on dealloc ;).

Alin