Displaying image retrieved from memory, as a button background
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??
Th开发者_运维问答anks in advance
P.S i am writing the above code in viewDidLoad method.
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
精彩评论