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 07-31-2009, 03:57 AM   #1 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 28
jetwilson is on a distinguished road
Default programmatically add animated UIImageView?

hi

I am adding a UIImageView programmatically into the view and filling it with an array of UImage objects and asking it to animate. However, nothing happens. Not even the first image displays in the view. I have previously been able to add one in using interface builder however that would prove impractical later on in my project.

This is my code:

Code:
aExplo.animationImages = [NSArray arrayWithObjects:
	 [UIImage imageNamed:@"RGB0001.png"],
	 [UIImage imageNamed:@"RGB0002.png"],
	 [UIImage imageNamed:@"RGB0003.png"],
	 [UIImage imageNamed:@"RGB0004.png"],
	 [UIImage imageNamed:@"RGB0005.png"],
	 [UIImage imageNamed:@"RGB0006.png"],
	 [UIImage imageNamed:@"RGB0007.png"],
	 [UIImage imageNamed:@"RGB0008.png"],
	 [UIImage imageNamed:@"RGB0009.png"],
	 [UIImage imageNamed:@"RGB0010.png"],
	 [UIImage imageNamed:@"RGB0011.png"],
	 [UIImage imageNamed:@"RGB0012.png"],
	 [UIImage imageNamed:@"RGB0013.png"],
	 [UIImage imageNamed:@"RGB0014.png"],
	 [UIImage imageNamed:@"RGB0015.png"]];
aExplo.animationRepeatCount = 1;
aExplo.animationDuration = 1;
aExplo.center = CGPointMake(100, 100);
[aExplo startAnimating];
[self.view addSubview:aExplo];
I set an @property (nonatomic, retain) and an @synthesize for the aExplo UIImageView. Is there anything im missing?

Thanks

Jetwilson
jetwilson is offline   Reply With Quote
Old 07-31-2009, 04:33 AM   #2 (permalink)
Registered Member
 
kelvinkao's Avatar
 
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
kelvinkao is on a distinguished road
Send a message via AIM to kelvinkao
Default

Did you set the frame for the UIImageView to display in?
kelvinkao is offline   Reply With Quote
Old 07-31-2009, 04:37 AM   #3 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 28
jetwilson is on a distinguished road
Default

Quote:
Originally Posted by kelvinkao View Post
Did you set the frame for the UIImageView to display in?
err... no.

I couldn't see anything in the documentation about setting a frame! What do I need to do to set a frame? do I need to call initWithFrame:?

EDIT: okay, i tried this code before I add images to the array to create a frame:
Code:
UIImage *image = [UIImage imageNamed:@"RGB0001.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
aExplo = [[UIImageView alloc] initWithFrame:frame];
One small problem - the program now crashes. Am I setting the frame correctly?

jetwilson

Last edited by jetwilson; 07-31-2009 at 04:49 AM. Reason: added sample code
jetwilson is offline   Reply With Quote
Old 07-31-2009, 06:22 AM   #4 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 11
imapps is on a distinguished road
Default

the frame looks fine. Where does your app crash? What does the debugger say?
imapps is offline   Reply With Quote
Old 07-31-2009, 06:40 AM   #5 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 28
jetwilson is on a distinguished road
Default

Quote:
Originally Posted by imapps View Post
the frame looks fine. Where does your app crash? What does the debugger say?
It crashes the next line after the frame code - the one where I create an array of images for the animationImages:

Code:
aExplo.animationImages = [NSArray arrayWithObjects:
	 [UIImage imageNamed:@"RGB0001.png"],
	 [UIImage imageNamed:@"RGB0002.png"],
	 [UIImage imageNamed:@"RGB0003.png"],
	 [UIImage imageNamed:@"RGB0004.png"],
	 [UIImage imageNamed:@"RGB0005.png"],
	 [UIImage imageNamed:@"RGB0006.png"],
	 [UIImage imageNamed:@"RGB0007.png"],
	 [UIImage imageNamed:@"RGB0008.png"],
	 [UIImage imageNamed:@"RGB0009.png"],
	 [UIImage imageNamed:@"RGB0010.png"],
	 [UIImage imageNamed:@"RGB0011.png"],
	 [UIImage imageNamed:@"RGB0012.png"],
	 [UIImage imageNamed:@"RGB0013.png"],
	 [UIImage imageNamed:@"RGB0014.png"],
	 [UIImage imageNamed:@"RGB0015.png"]];
The last thing it says in the debugger window is objc_msgsend if that helps.

thanks

jetwilson
jetwilson is offline   Reply With Quote
Old 07-31-2009, 06:51 AM   #6 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 11
imapps is on a distinguished road
Default

You're missing a nil after the last object in array. arrayWithObjects: requires a comma separated array items ending with nil like this:

Code:
aExplo.animationImages = [NSArray arrayWithObjects:
	 [UIImage imageNamed:@"RGB0001.png"],
	 [UIImage imageNamed:@"RGB0002.png"],
	 [UIImage imageNamed:@"RGB0003.png"],
	 [UIImage imageNamed:@"RGB0004.png"],
	 [UIImage imageNamed:@"RGB0005.png"],
	 [UIImage imageNamed:@"RGB0006.png"],
	 [UIImage imageNamed:@"RGB0007.png"],
	 [UIImage imageNamed:@"RGB0008.png"],
	 [UIImage imageNamed:@"RGB0009.png"],
	 [UIImage imageNamed:@"RGB0010.png"],
	 [UIImage imageNamed:@"RGB0011.png"],
	 [UIImage imageNamed:@"RGB0012.png"],
	 [UIImage imageNamed:@"RGB0013.png"],
	 [UIImage imageNamed:@"RGB0014.png"],
	 [UIImage imageNamed:@"RGB0015.png"],
         nil];
imapps is offline   Reply With Quote
Old 07-31-2009, 07:16 AM   #7 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 28
jetwilson is on a distinguished road
Default

Quote:
Originally Posted by imapps View Post
You're missing a nil after the last object in array. arrayWithObjects: requires a comma separated array items ending with nil like this:

Code:
aExplo.animationImages = [NSArray arrayWithObjects:
	 [UIImage imageNamed:@"RGB0001.png"],
	 [UIImage imageNamed:@"RGB0002.png"],
	 [UIImage imageNamed:@"RGB0003.png"],
	 [UIImage imageNamed:@"RGB0004.png"],
	 [UIImage imageNamed:@"RGB0005.png"],
	 [UIImage imageNamed:@"RGB0006.png"],
	 [UIImage imageNamed:@"RGB0007.png"],
	 [UIImage imageNamed:@"RGB0008.png"],
	 [UIImage imageNamed:@"RGB0009.png"],
	 [UIImage imageNamed:@"RGB0010.png"],
	 [UIImage imageNamed:@"RGB0011.png"],
	 [UIImage imageNamed:@"RGB0012.png"],
	 [UIImage imageNamed:@"RGB0013.png"],
	 [UIImage imageNamed:@"RGB0014.png"],
	 [UIImage imageNamed:@"RGB0015.png"],
         nil];

Thank you! It worked perfectly! I cant believe I missed something so simple!

Jetwilson
jetwilson is offline   Reply With Quote
Old 07-31-2009, 07:19 AM   #8 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 11
imapps is on a distinguished road
Default

np
Glad I could help.
imapps is offline   Reply With Quote
Old 07-31-2009, 08:46 AM   #9 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 60
JeromeC is on a distinguished road
Default

meh, I come too late!
Anyhow I just finished a tutorial on just that topic: Link
__________________
JeromeC is offline   Reply With Quote
Old 07-31-2009, 01:53 PM   #10 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 28
jetwilson is on a distinguished road
Default new problem!

The previous problem was me just starting at the basics, once I had that sorted I thought I would expand and make this code more reusable. But yet another block has reared its ugly head.

I thought, to make the animation code more reusable and efficient I would create a UIView object and then create instances of that class to use within the main view.

This is the code within the explosionView.m UIView object file:

Code:
- (id)init {
	UIImage *image = [UIImage imageNamed:@"RGB0001.png"];
	CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
	aExplosionView = [[UIImageView alloc] initWithFrame:frame];
	[image release];
	
	self.opaque = NO;
	aExplosionView.animationImages = [NSArray arrayWithObjects:
							[UIImage imageNamed:@"RGB0001.png"],
							[UIImage imageNamed:@"RGB0002.png"],
							[UIImage imageNamed:@"RGB0003.png"],
							[UIImage imageNamed:@"RGB0004.png"],
							[UIImage imageNamed:@"RGB0005.png"],
							[UIImage imageNamed:@"RGB0006.png"],
							[UIImage imageNamed:@"RGB0007.png"],
							[UIImage imageNamed:@"RGB0008.png"],
							[UIImage imageNamed:@"RGB0009.png"],
							[UIImage imageNamed:@"RGB0010.png"],
							[UIImage imageNamed:@"RGB0011.png"],
							[UIImage imageNamed:@"RGB0012.png"],
							[UIImage imageNamed:@"RGB0013.png"],
							[UIImage imageNamed:@"RGB0014.png"],
							[UIImage imageNamed:@"RGB0015.png"],
							nil];
		
	aExplosionView.animationRepeatCount = 1;
	aExplosionView.animationDuration = 1;
	aExplosionView.center = CGPointMake(0, 0);
	[aExplosionView startAnimating];
	
	return self;
}
pretty much exactly the same as before.

and in my mainViewController.m I do this to create an instance of the explosionView class in viewDidLoad:

Code:
UIImage *image = [UIImage imageNamed:@"RGB0001.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
explosionView *explo = [[explosionView alloc] initWithFrame:frame];
[image release];explo.center = CGPointMake(100, 100);
[self.view addSubview:explo];
I have done this before with UIImages for non-animation images - but when I tried with this animating UIImageView, a black square where my animation should be displayed is displayed. No crash, no nothing. Just a black square. Not terribly useful!

Anyone know what's happening?

Thanks

Jetwilson

Last edited by jetwilson; 08-01-2009 at 03:41 AM. Reason: added initWithFrame: code and question
jetwilson is offline   Reply With Quote
Old 09-20-2010, 04:48 PM   #11 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: PK
Posts: 27
d4devil is on a distinguished road
Default Animated Image is not placed on the location i mentioned

Hi i have used the coding technique you mentioned above, My image has started animation. But it is not well placed at the location i mentioned

This is the code i am using
UIImage *myBalloonImage=[UIImage imageNamed:@"ABalloon.png"];
UIImageView *myBalloonAnimatedView = [[UIImageView alloc] initWithImage:myBalloonImage];

CGRect frameBalloon = CGRectMake(60, 20,myBalloonImage.size.height, myBalloonImage.size.width);
myBalloonAnimatedView = [[UIImageView alloc] initWithFrame:frameBalloon];
[myBalloonImage release];
NSArray *myBalloonImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"ABalloon.png"],
[UIImage imageNamed:@"CBalloon.png"],
[UIImage imageNamed:@"EBalloon.png"],
nil];

myBalloonAnimatedView.animationImages = myBalloonImages;
myBalloonAnimatedView.animationDuration = 2.0;
myBalloonAnimatedView.animationRepeatCount = 0;
[myBalloonAnimatedView startAnimating];
myBalloonAnimatedView.center=CGPointMake(160, 160);
[self.view addSubview:myBalloonAnimatedView];
[myBalloonAnimatedView release];

Now kindly tell me in hurry where i am doing mistake. According to me , There is some problem i am facing in making frames.
d4devil is offline   Reply With Quote
Reply

Bookmarks

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: 326
11 members and 315 guests
chiataytuday, coolman, givensur, guusleijsten, ipodphone, jbro, mediaspree, mottdog, mtl_tech_guy, Punkjumper, vilisei
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,882
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

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