Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ionini

macrumors newbie
Original poster
Aug 29, 2011
13
0
so im making this app that uses a NSTimer of 0.05 secs and what it does is this:
Code:
if (x4<=17) {
      num = [NSString stringWithFormat:@"%i", x4];
      imagen =  [NSString stringWithFormat:@"%@%@", @"image", num];
      imagen =  [NSString stringWithFormat:@"%@%@", imagen, @".png"];
      imageview.image = [UIImage imageNamed:imagen];
      x4++;
      x=0;
}
if (x4==17){
      x4=0;
}
(i have like 8 of that, each one with different images) And this uses up like 70MB of memory!!!
(all the image files are less than 10MB)
HELP!!!
 
You might want to describe what it is you are trying to do. Someone might have a better suggestion on how to solve the problem.

That 10MB is for images that are compressed. I would think they need to be uncompressed to be used in an imageview.
 
The imageNamed: API is supposed to cache images so that they can be used again without unboxing and uncompressing. For transient images that you want to be switching out over-and-over, use initWithCGImage:scale:eek:rientation: and that should help with the memory use.

Why are you not using the animationImages property of UIImageView?

Nevermind. You would have to front-load each image before you add it to the animationImages array.

A stylish approach might be to try using GCD instead of NSTimer. You can use GCD to run blocks of code at arbitrary time intervals, and you can also use GCD to load the image from the bundle while you are waiting for the next turn around the cycle.
 
Last edited:
Less then 10 mb? jesus christ. Even with a bloody nice library, which has only -1 - Current - +1 Images rendered. It's still hard with 4mb+ images..
You should try to scale them a bit, that's a starter.
 
is animation better?

The imageNamed: API is supposed to cache images so that they can be used again without unboxing and uncompressing. For transient images that you want to be switching out over-and-over, use initWithCGImage:scale:eek:rientation: and that should help with the memory use.

Why are you not using the animationImages property of UIImageView?

Nevermind. You would have to front-load each image before you add it to the animationImages array.

A stylish approach might be to try using GCD instead of NSTimer. You can use GCD to run blocks of code at arbitrary time intervals, and you can also use GCD to load the image from the bundle while you are waiting for the next turn around the cycle.
is animationImages better?
 
Try it.

And you still haven't explained your goal. That can go along way in receiving quality answers.

i just wan to play some animations, but just realized animationimage is no good for me. can you help me with imageWithCGImage, i dont know how to use it.
is it posible that im loading the same image file repeatedly to the cache?
 
Last edited:
i just wan to play some animations, but just realized animationimage is no good for me.

Based on your original post, it sounds like you want to loop through 18 images to animate them. I think the animationImages property should handle that fine. I'm not sure why North Bronson was against preloading the images. He did mention to rescale them so they take a reduced memory size.


can you help me with imageWithCGImage, i dont know how to use it.
is it posible that im loading the same image file repeatedly to the cache?

I dont' see why you are asking about imageWithCGImage. That seems to be another path. Are you creating images from scratch, or thinking of resizing what you are reading in? If it is resizing you are thinking about, then perhaps the method North Bronson mentioned will suffice.

I'm not sure how the cache works when loading the same image into two different objects via the imageNamed: method. You haven't explained why you would load the same image multiple times.


Here is some pseudocode that maybe helpful. This is via reading the documentation. I think I did this once way back when. ;)

Code:
NSMutableArray * myAnimationImages = [NSMutableArray new];
//Your loop here to preload the images, rescale, and place into an array for animation use.
UIImage * image = [UIImage imageNamed:imagen]; // non-retained
CGFloat myScaleFactor = 0.5; // Perhaps calculate this value for each image.
UIImage * scaledImage = [UIImage imageWithCGImage: image.CGImage scale: myScaleFactor orientation: UIImageOrientationUp];
[myAnimationImages addObject: scaledImage];
// finish of loop

UIImageView * myAnimationView = [[UIImageView alloc] initWithImage: [myAnimationImages objectAtIndex: 0];
myAnimationView.animationImages = myAnimationImages;
[myAnimationImages release];
myAnimationView.animationDuration = 0.9; // = 18 * .05
myAnimationView.animationRepeatCount = 0; // loop continuously
[myAnimationView  startAnimating];
 
Based on your original post, it sounds like you want to loop through 18 images to animate them. I think the animationImages property should handle that fine. I'm not sure why North Bronson was against preloading the images. He did mention to rescale them so they take a reduced memory size.




I dont' see why you are asking about imageWithCGImage. That seems to be another path. Are you creating images from scratch, or thinking of resizing what you are reading in? If it is resizing you are thinking about, then perhaps the method North Bronson mentioned will suffice.

I'm not sure how the cache works when loading the same image into two different objects via the imageNamed: method. You haven't explained why you would load the same image multiple times.


Here is some pseudocode that maybe helpful. This is via reading the documentation. I think I did this once way back when. ;)

Code:
NSMutableArray * myAnimationImages = [NSMutableArray new];
//Your loop here to preload the images, rescale, and place into an array for animation use.
UIImage * image = [UIImage imageNamed:imagen]; // non-retained
CGFloat myScaleFactor = 0.5; // Perhaps calculate this value for each image.
UIImage * scaledImage = [UIImage imageWithCGImage: image.CGImage scale: myScaleFactor orientation: UIImageOrientationUp];
[myAnimationImages addObject: scaledImage];
// finish of loop

UIImageView * myAnimationView = [[UIImageView alloc] initWithImage: [myAnimationImages objectAtIndex: 0];
myAnimationView.animationImages = myAnimationImages;
[myAnimationImages release];
myAnimationView.animationDuration = 0.9; // = 18 * .05
myAnimationView.animationRepeatCount = 0; // loop continuously
[myAnimationView  startAnimating];

it gives me a SIGBRT signal at this line:

[myAnimationView startAnimating];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.