Problem with memory!!!

Discussion in 'iOS Programming' started by ionini, Sep 15, 2011.

  1. ionini macrumors newbie

    Aug 29, 2011
    so im making this app that uses a NSTimer of 0.05 secs and what it does is this:
    if (x4<=17) {
          num = [NSString stringWithFormat:@"%i", x4];
          imagen =  [NSString stringWithFormat:@"%@%@", @"image", num];
          imagen =  [NSString stringWithFormat:@"%@%@", imagen, @".png"];
          imageview.image = [UIImage imageNamed:imagen];
    if (x4==17){
    (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)
  2. xStep macrumors 68000

    Jan 28, 2003
    Less lost in L.A.
    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.
  3. North Bronson, Sep 15, 2011
    Last edited: Sep 15, 2011

    North Bronson macrumors 6502

    Oct 31, 2007
    San José
    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.
  4. jnoxx macrumors 65816


    Dec 29, 2010
    Aartselaar // Antwerp // Belgium
    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.
  5. ionini thread starter macrumors newbie

    Aug 29, 2011
    is animation better?

    is animationImages better?
  6. xStep macrumors 68000

    Jan 28, 2003
    Less lost in L.A.
    Try it.

    And you still haven't explained your goal. That can go along way in receiving quality answers.
  7. ionini, Sep 16, 2011
    Last edited: Sep 16, 2011

    ionini thread starter macrumors newbie

    Aug 29, 2011
    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?
  8. xStep macrumors 68000

    Jan 28, 2003
    Less lost in L.A.
    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. ;)

    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];
  9. ionini thread starter macrumors newbie

    Aug 29, 2011
    it gives me a SIGBRT signal at this line:

    [myAnimationView startAnimating];
  10. xStep macrumors 68000

    Jan 28, 2003
    Less lost in L.A.
    Did you just cut * paste my code or did you add that view to another view for display. We can't tell much without your new code.

Share This Page