I have got this code which creates an image:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
background = [[UIImageView alloc]initWithFrame:CGRectMake(150, 60, 180, 180)];
result = [[UIImageView alloc]initWithFrame:CGRectMake(150, 60, 180, 180)];
[background setImage:[UIImage imageNamed:[defaults objectForKey:@"colour"]]];
[self.view addSubview:background];
[self.view insertSubview:result aboveSubview:background];
This is in my viewWillAppear. When I press a button, this happens:
- (IBAction)oneToSix {
int rNumber = (arc4random() % 6) + 1;
[result setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@", rNumber]]];
}
But it gets this fatal error: Thread one: Program received signal: "EXC_BAD_ACCESS". on the setImage part when I press the button. What is the problem? I am new to Objective C.
I think the problem is string format in NSString, you should change the code to :
as rNumber is an int.
And you should release the background and result, as viewWillAppear will tend to be called every time the view is appear so will result a memory leak.