title: App crashes when UIImageView.image is set 2-3 times
body: <p>My app crashes when I set the image-property on a UIImageView three times in a row (sometimes two times is enough). A few times I've seen a memory warning before the app closes, but most of the times it just collapses. The app does not crash in the simulator, so I'm quite sure it is a memory problem.</p>
  
  <p>Here's the code I use when setting the image property:</p>
  
  <pre><code>-(void)changeBgPictureTo:(UIImage *)img
  {
      [self.backgroundImage setImage:img];
  }
  </code></pre>
  
  <p>The UIImages is allocated with the <code>[UIImage imageWithData:]</code> method:</p>
  
  <pre><code>[UIImage imageWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:type]]];
  </code></pre>
  
  <p>The image is set correctly the first two times, but crashes on the third time. It has nothing to do with a specific image file. I've tried with 10 different images and it makes no difference.</p>
  
  <p>How do I make the UIImageView unload previous loaded images?</p>
  
  <p><strong>EDIT:</strong>
  Okay, I've been asked for the whole code so here it goes:</p>
  
  <p>I'm working with a class which looks like this:</p>
  
  <pre><code>@interface MyImage : NSObject
  {
      UIImage* image;
      int imgId;
      NSArray* colors; //contains 'UIColor' objects.
  }
  
  @property (nonatomic, retain) UIImage* image;
  @property (nonatomic, retain) NSArray* colors;
  @property (nonatomic, readwrite) int imgId;
  
  -(id)initWithFileName:(NSString*)fileName withType:(NSString*)type andId:(int)imgId andColors:(NSArray*)colorArray;
  
  @end
  </code></pre>
  
  <p>Here is the init implementation:</p>
  
  <pre><code>-(id)initWithFileName:(NSString*)fileName withType:(NSString*)type andId:(int)imageId andColors:(NSArray*)colorArray
  {
       self = [super init];
  
       if (self)
       {
           self.colors = [NSArray arrayWithArray:colorArray]; 
           self.imgId = imageId;
           self.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:type]]];
       }   
  
  return self;
  </code></pre>
  
  <p>}</p>
  
  <p>I have a datacontroller which has a list of 'MyImage' objects which is added in a loop with this call:</p>
  
  <pre><code>[self.myImages addObject:[[MyImage alloc] initWithFileName:[NSString stringWithFormat:@"0000%d", i] withType:@"jpg" andId:i andColors:colors]];
  </code></pre>
  
  <p>I have 9 images which is named 00001.jpg, 00002.jpg, ....., 00008.jpg.</p>
  
  <p>This data controller has a method like this:</p>
  
  <pre><code>-(MyImage *)getImageWithId:(int)imgId
  {
      for (MyImage* img in self.myImages)
      {
          if (img.imgId == imgId)
              return img;
      }
      return nil;
  }
  </code></pre>
  
  <p>The getImageWithId method is called like this:</p>
  
  <pre><code>-(void)btnPushed:(id)sender
  {
       [self.delegate changeBgPictureTo:[self.imgDataController getImageWithId:((UIButton*)sender).tag]];    
  }  
  </code></pre>
  
  <p>The changeBgPictureTo method is the method which makes the setting of the image-property:</p>
  
  <pre><code>-(void)changeBgPictureTo:(MyImage *)img
  {
      NSLog(@"Setting image: %d", img.imgId);
      [self.backgroundImage setImage:img.image];    
  }
  </code></pre>
  
  <p>The log prints "Setting image: 0000X:" three times, but crashes shortly after the third print.</p>