Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-21-2011, 09:14 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 59
kend0g187 is on a distinguished road
Default 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?
kend0g187 is offline   Reply With Quote
Old 02-21-2011, 09:17 PM   #2 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

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];
}
Objective Zero is offline   Reply With Quote
Old 02-21-2011, 09:45 PM   #3 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 59
kend0g187 is on a distinguished road
Default

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.
kend0g187 is offline   Reply With Quote
Old 02-21-2011, 09:46 PM   #4 (permalink)
Registered Member
 
Objective Zero's Avatar
 
Join Date: Oct 2010
Posts: 1,210
Objective Zero is on a distinguished road
Default

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?
Objective Zero is offline   Reply With Quote
Reply

Bookmarks

Tags
image, management, memory, swap, uiimageview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» 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
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:12 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0