views:

26

answers:

2

I'm working on a project for the iPad where I'm trying to simulate the rotation of a 3D-body with touch by switching pre-rendered images.

I'm not the sharpest man when it comes to memory management so I wonder if anyone has any tips on how to optimize this.

My solution now looks something like this:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [[event allTouches] anyObject];
CGPoint location = [myTouch locationInView:self.view];
int pictureIndex;
//Rough mapping of image to point on screen
if (location.x <= 384 ) {
    pictureIndex = (384 - location.x)/(768/rotatingPictures)+1;
}
else {
    pictureIndex = rotatingPictures - (location.x-384)/(768/rotatingPictures)+1;
}
[theImageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"image_%d.png", pictureIndex]]];

}

Is it efficient to load the images this way? Would it be better to load the images to an array first or something like that?

I would prefer to have all loading time at once and not while rotating.

A: 

Well optimization is not really important so long as you're not loading the picture every frame. I don't profess to be an expert on this subject but it might be a good idea to keep tabs on all loaded images in order to avoid reloading them later.

Neil
A: 

The first critical piece of information is that imageNamed is hopeless! It is well-known to be a dog. It's neither one thing or the other; it doesn't cache well and it tends to build up too much memory use. (Google up 100s of articles on this.) So, that's likely out.

You will have to tell us exactly how many images you have, and exactly how big (KB or MB) each image is.

Only then can one decide a successful strategy.

To swap from one large image to another "properly", if you are swapping huge images over and over and if you have to worry about memory, you kind of do this...

[hugeImage.image release];
hugeImage.image = [[UIImage alloc] initWithContentsOfFile:
           [[NSBundle mainBundle]
           pathForResource:@"bodyPart"
           ofType:@"png"]];

that will properly get rid of the previous image and bring in the next one. NEVER, EVER use imageNamed -- it is specifically designed only for small (i.e. icon sized) interface-use (i.e., buttons, icons, etc) images.

However again, nobody can help you until we know the precise number of images, and the precise size, and indeed how often per second or per minute you expect the images to be swapped. Hope it helps!

----- You went on to ask ...

"89 .png pictures ranging in size from about 70 K to 100 K"

Good news, you're going to have NO PROBLEM WHATSOEVER. You can easily load those, and you can even do it fast enough in real time to make a 20 - 30 fps animation!!

Simply do exactly what I say above each time you swap from one to another, and you're done.

(For the record, be careful about sort of loading one first when you are initialising, so that the code fragment above wprks properly the "first time," you know. And if you can release the last one when you're done.) Cheers!

I hope this helps and please vote for my answer! :-)

Joe Blow
89 .png pictures ranging in size from about 70 K to 100 K. I will try initWithContentsOfFile right away
Jonatan
answered above in edit
Joe Blow