views:

40

answers:

2

Hi all,

I have a button on my view. I want to display an image as the background on it. The image is previously stored on the memory by the same application.

`NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *uniquePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:"Image.jpg"]; UIImage *image = [UIImage imageWithContentsOfFile: uniquePath];

[btnImage setBackgroundImage:image forState:UIControlStateNormal];`

Every-time I execute the code, the application crashes after the end of the method that contains the above code. I tried debugging, and found that every step executes perfectly, but the application crashes at the end. I guess that problem is with setting the image as the button background. Can anyone help me out of this??

Thanks in advance

P.S i am writing the above code in viewDidLoad method.

A: 

If you were using Interface Builder you can simply set the Image from one of the properties; this is assuming the image were added to your bundle.

Otherwise if the image is loaded at run-time and/or you're doing everything in code, which I 'm guessing you are.. I think the problem is with the "NSString *uniquePath ..." line. Have you run the code in the debugger? Is the variable uniquePath nil?

UPDATE: browsing this question again, I saw the problem.

You're not retaining a copy of your image instance variable - do this before returning from -viewDidLoad. Remember to release it in -viewDidUnload

petert
A: 

Hi Petert,

Thanks for the reply. You have guessed it right. I am doing everything programmatically.

I have tried debugging, and there's always documents directory path in the uniquePath variable. Actually the code executes fine on every step, but as soon as the viewDidLoad ends, the application crashes.

Hope you could be able to help me further.

socialCircus