02-21-2011, 09:14 PM
#1 (permalink )
Registered Member
Join Date: Nov 2010
Posts: 59
swap background image?
I'm trying to swap the background image when the user swipes the screen. I have this code:
Code:
- (void)viewDidLoad {
UIImageView *background = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"img1.png"]];
[background setFrame: CGRectMake( 0.0, 44.0, 320.0, 366.0 )];
[self.view insertSubview: background atIndex: 0];
[background release];
}
My swipe gesture recognizer calls this function:
Code:
- (void) swapBackgroundRight {
UIImageView *newBackground = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"img2.png"]];
[newBackground setFrame: CGRectMake( -320.0, 44.0, 320.0, 366.0 )];
[self.view insertSubview: newBackground atIndex: 1];
[newBackground release];
[UIView beginAnimations: @"Slide In" context: nil];
[UIView setAnimationDuration: 0.3];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
[[self.view.subviews objectAtIndex: 0] setFrame: CGRectMake( 320.0, 44.0, 320.0, 366.0 )];
[newBackground setFrame: CGRectMake( 0.0, 44.0, 320.0, 366.0 )];
[UIView commitAnimations];
[[self.view.subviews objectAtIndex: 0] performSelector: @selector(removeFromSuperview) withObject: nil afterDelay: 0.3];
}
When I step through the code, every line executes as expected, but after the method finishes, I get EXC_BAD_ACCESS. This is driving me crazy. Any ideas?
02-21-2011, 09:17 PM
#2 (permalink )
Registered Member
Join Date: Oct 2010
Posts: 1,210
Try this for your second code set:
Code:
- (void) swapBackgroundRight {
UIImageView *newBackground = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"img2.png"]];
[newBackground setFrame: CGRectMake( -320.0, 44.0, 320.0, 366.0 )];
[self.view insertSubview: newBackground atIndex: 1];
[UIView beginAnimations: @"Slide In" context: nil];
[UIView setAnimationDuration: 0.3];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
[[self.view.subviews objectAtIndex: 0] setFrame: CGRectMake( 320.0, 44.0, 320.0, 366.0 )];
[newBackground setFrame: CGRectMake( 0.0, 44.0, 320.0, 366.0 )];
[UIView commitAnimations];
[[self.view.subviews objectAtIndex: 0] performSelector: @selector(removeFromSuperview) withObject: nil afterDelay: 0.3];
[newBackground release];
}
02-21-2011, 09:45 PM
#3 (permalink )
Registered Member
Join Date: Nov 2010
Posts: 59
Didn't work. Based on my understanding of memory management, when I add 'newBackground' to the view it increases it's retain count to 2, so if I release it right away it still has a retain count of 1 and should still stick around.
The weird thing is every once-in-a-while this code will work. The full code actually swaps between between 7 images like so:
Code:
if( backgroundIndex > 1 )
backgroundIndex--;
else
backgroundIndex = kNumBackgrounds;
NSString *newName = [NSString stringWithFormat: @"texture%d.png", backgroundIndex];
UIImageView *newBackground = [[UIImageView alloc] initWithImage: [UIImage imageNamed: newName]];
[newName release];
In the rare case that the code runs once, it will fail on the 7th run the I try to set the first image back as the backgound. Don't know if that helps anyone. It just makes me more confused.
Wow, as I was typing that I realized I'm releasing a string that I never allocated. Been playing with my code for about 4 hours and never noticed it.
Last edited by kend0g187; 02-21-2011 at 09:47 PM .
Reason: I'm an idiot.
02-21-2011, 09:46 PM
#4 (permalink )
Registered Member
Join Date: Oct 2010
Posts: 1,210
What if you just take out [newBackground release];
Will that do it? Also did you try autorelease?
What does the console say when it crashes?
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 378
17 members and 361 guests
Absentia , apatsufas , BinHex , cpsclicker , dre , Error404 , Gaz , gmarro , jeroenkeij , Kirkout , mottdog , Music Man , PavelMik , teebee74 , whitey99 , Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker