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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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 11-25-2009, 05:07 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 8
Default Multiple UIImageViews slowing down app badly

Hi There,

I'm fairly new to iPhone development, coming from a background of Flash development, and am struggling with something I can easily achieve with no performance issues at all in Flash.

I am creating multiple instances of a class which loads a UIImageView. The code I use to create the multiple instances of this class is below:

Code:
for (NSInteger i=0; i<20; i++) {
		FireBall *fireBall = [FireBall alloc];
		fireBall.vx = power;
		fireBall.vy = power * 0.75;
		fireBall.gravity = 0.7;
		fireBall.baseClass = self;
		UIImageView *fireballImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Fireball_1.png"]];
		fireballImageView.frame = CGRectMake(70, 205, 23, 23);
		[self.view addSubview:fireballImageView];
		fireBall.fireballImageView = fireballImageView;
		[fireBall startFireBall];
	}
When I create these an fire them off in one go the app slows dopwn horrendously.

The code within the actual class file itself is below:

FireBall.m:

Code:
#import "FireBall.h"
#import "WizardViewController.h"

@implementation FireBall

@synthesize vx;
@synthesize vy;
@synthesize gravity;
@synthesize moveTimer;
@synthesize fireballImageView;
@synthesize baseClass;

- (void)startFireBall {
	moveTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 / 30.0 target:self selector:@selector(moveFireBall:) userInfo:nil repeats:YES];
}

- (void)moveFireBall:(NSTimer *)timer {
	vy -= gravity;
	fireballImageView.center = CGPointMake(fireballImageView.center.x + vx, fireballImageView.center.y - vy);
	if (fireballImageView.center.x > 500) {
		[self removeFireBall];
	} else if (fireballImageView.center.y > 260) {
		[(WizardViewController*)baseClass createExplosion:fireballImageView.center];
		[self removeFireBall];
	}
}

- (void)removeFireBall {
	// remove the image itself
	[fireballImageView release];
	[fireballImageView removeFromSuperview];
	// stop the timer
	[(NSTimer*)moveTimer invalidate];
	[self release];
}

@end
So I'm passing a reference to my main view controller class (baseClass) through and also a reference to the UIImageView I'm associating with the class file through too. I'm then moving the UIImageView 30 times a second for each of the 20 UIImageViews.

What am I doing to make it so horribly slow. It works fine with one or two of these going, but with 20 it's horrible and jerky.

Thanks for any help anyone can offer. I'm really stuck.

Cheers,

Ian
iamstuck is offline   Reply With Quote
Old 11-25-2009, 05:17 PM   #2 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: London
Posts: 226
Default

The thing you have to remember is that UIImageViews are flexible (ie. complex) user interface components. They're not really meant to act as game sprites.

To achieve what you want to do at a reasonable frame rate, look at either animating at the CALayer level, or use an OpenGL-based sprite library like Cocos2d.

Oh, and when you alloc an objective-C object, you should always also call init on it, even if you haven't written any init code of your own, since this sets up its inherited properties.
__________________
SimCap - Simple iPhone and iPad Simulator screen capture
_sjc_ is offline   Reply With Quote
Old 11-25-2009, 05:36 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 8
Default

Thanks very much for the response. I'd been following this tutorial initially:

Gravity Tutorial for iPhone Part 1 | BIT-101 Blog

Which made a lot of sense to me but it only animates one UIImageView (which works fine).

Thanks for the pointer to the Cocos2d code. I think I shall investigate.

Thanks also for the tip about the [init] - I've put that in there now.

All the best,

Ian
iamstuck is offline   Reply With Quote
Reply

Bookmarks

Tags
ball, class, move, multiple, 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: 265
20 members and 245 guests
ADY, AragornSG, Bertrand21, Dani77, Dattee, Duncan C, fkmtc, HDshot, HemiMG, iDifferent, JasonR, macquitzon216, mer10, prchn4christ, Rudy, sacha1996, sneaky, spiderguy84, Sunny46, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,231
Posts: 380,768
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

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