title: Xcode: UIScrollView image gallery, having memory problems
body: <p>I'm Trying to develop an <code>UIScrollView</code> Based Image gallery.</p>
  
  <p>So let me explain what i'm trying to achieve here:</p>
  
  <p>I want to a sliding presentation, that can show upto 140 image. ( You can swipe back and forth )
  I've found information on the web, and i've been told the best way to do this is with a <code>UIScrollview</code> which has 3 <code>UIImageViews</code> which you create and remove from superview.</p>
  
  <p>So i managed to create such a "sliding image gallery" with some help from a few tutorials :).
  I've managed to upload the application to the ipad, start up the application and run it.</p>
  
  <p>after i viewed about 50-70 slides the app crashes ( out of memory). My knowledge of Obj. C isn't that great .</p>
  
  <p>You'll find the code below: it Prob. has something to do with releasing the images.
  Improvements to the code would be really helpful</p>
  
  <pre><code>#import "ParatelPresentationViewController.h"
  
  //Define the UIView ( we need 3 Image Views left, mid right);
  @interface SlideShowView : UIView
  {
   NSArray * mImages;
  
   UIImageView * mLeftImageView;
   UIImageView * mCurrentImageView;
   UIImageView * mRightImageView;
  
   NSUInteger mCurrentImage;
  
   BOOL mSwiping;
   CGFloat mSwipeStart;
  }
  
  - (id)initWithImages:(NSArray *)inImages;
  
  @end // SlideShowView
  
  #pragma mark -
  
  @implementation SlideShowView
  
  
  - (UIImageView *)createImageView:(NSUInteger)inImageIndex
  {
   if (inImageIndex &gt;= [mImages count])
   {
    return nil;
   }
  
   UIImageView * result = [[UIImageView alloc] initWithImage:[mImages objectAtIndex:inImageIndex]];
   result.opaque = YES;
   result.userInteractionEnabled = NO;
   result.backgroundColor = [UIColor blackColor];
   result.contentMode = UIViewContentModeScaleAspectFit;
   result.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ;
  
   return result;
  }
  
  - (id)initWithImages:(NSArray *)inImages
  {
   if (self = [super initWithFrame:CGRectZero])
   {
    mImages = [inImages retain];
  
    NSUInteger imageCount = [inImages count];
    NSLog(@"hoeveel foto's: %i");
    if (imageCount &gt; 0)
    {
     mCurrentImageView = [self createImageView:0];
     [self addSubview:mCurrentImageView];
  
     if (imageCount &gt; 1)
     {
      mRightImageView = [self createImageView:1];
      [self addSubview:mRightImageView];
     }
  
    }
  
    self.opaque = YES;
    self.backgroundColor = [UIColor blueColor];
    self.contentMode = UIViewContentModeScaleAspectFit;
    self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
   }
  
   return self;
  }
  
  - (void)dealloc
  {
   [mImages release];
   [super dealloc];
  }
  
  
  
  - (void)layoutSubviews
  {
   if (mSwiping)
    return;
  
   //CGSize contentSize = self.frame.size; // Enable when you use content.width/height
   //self.backgroundColor = [UIColor redColor];
   mLeftImageView.frame = CGRectMake(-1024, 0.0f, 1024, 748);//  (-1024, 0.0f, 1024, 748) can be replaced by  (-contentSize.width, 0.0f, contentSize.width, contentSize.height);
   mCurrentImageView.frame = CGRectMake(0.0f, 0.0f, 1024, 748);
   mRightImageView.frame = CGRectMake(1024, 0.0f,  1024, 748);
  }
  
  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  {
   if ([touches count] != 1)
    return;
  
   mSwipeStart = [[touches anyObject] locationInView:self].x;
   mSwiping = YES;
  
   mLeftImageView.hidden = NO;
   mCurrentImageView.hidden = NO;
   mRightImageView.hidden = NO;
  }
  
  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  {
   if (! mSwiping || [touches count] != 1)
    return;
  
   CGFloat swipeDistance = [[touches anyObject] locationInView:self].x - mSwipeStart;
  
   //CGSize contentSize = self.frame.size;
  
   mLeftImageView.frame = CGRectMake(swipeDistance - 1024, 0.0f, 1024, 748);
   mCurrentImageView.frame = CGRectMake(swipeDistance, 0.0f, 1024, 748);
   mRightImageView.frame = CGRectMake(swipeDistance + 1024, 0.0f, 1024, 748);
  }
  
  - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  {
   if (! mSwiping)
    return;
  
   //CGSize contentSize = self.frame.size;
  
   NSUInteger count = [mImages count];
  
   CGFloat swipeDistance = [[touches anyObject] locationInView:self].x - mSwipeStart;
   if (mCurrentImage &gt; 0 &amp;&amp; swipeDistance &gt; 50.0f)
   {
    mRightImageView.image = nil;
    //[mRightImageView.image release];
    [mRightImageView removeFromSuperview];
    [mRightImageView release];
    //mRightImageView = nil;
    //NSLog(@"Count of mRight : %i",[mRightImageView retainCount]);
  
  
    mRightImageView = mCurrentImageView;
    mCurrentImageView = mLeftImageView;
  
    mCurrentImage--;
    if (mCurrentImage &gt; 0)
    {
     mLeftImageView = [self createImageView:mCurrentImage - 1];
     mLeftImageView.hidden = YES;
  
     [self addSubview:mLeftImageView];
    }
    else
    {
     mLeftImageView = nil;
    }
   }
   else if (mCurrentImage &lt; count - 1 &amp;&amp; swipeDistance &lt; -50.0f)
   {
    mLeftImageView.image = nil;
    //[mLeftImageView.image release];
    [mLeftImageView removeFromSuperview];
    [mLeftImageView release];
    //mLeftImageView = nil;
  
    mLeftImageView = mCurrentImageView;
    mCurrentImageView = mRightImageView;
  
    mCurrentImage++;
    if (mCurrentImage &lt; count - 1)
    {
     mRightImageView = [self createImageView:mCurrentImage + 1];
     mRightImageView.hidden = YES;
  
     [self addSubview:mRightImageView];
     NSLog(@"Count of mRight : %i",[mRightImageView.image retainCount]);
    }
    else
    {
     mRightImageView = nil;
    }
   }
  
   [UIView beginAnimations:@"swipe" context:NULL];
   [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
   [UIView setAnimationDuration:0.3f];
  
   mLeftImageView.frame = CGRectMake(-1024, 0.0f, 1024, 748);
   mCurrentImageView.frame = CGRectMake(0.0f, 0.0f, 1024, 748);
   mRightImageView.frame = CGRectMake(1024, 0.0f, 1024, 748);
  
   [UIView commitAnimations];
  
   mSwiping = NO;
  }
  
  
  @end // SlideShowView
  
  
  #pragma mark -
  
  
  @implementation ParatelPresentationViewController
  
  - (id)init
  {
   if (self = [super initWithNibName:nil bundle:nil])
   {
  
    NSMutableArray *Displayimages = [[NSMutableArray alloc]init];
    int i;
    for(i=0 ; i&lt;139 ; i++) {
  
  
     NSString *tempString = [NSString stringWithFormat:@"Dia%d", i+1];
     NSLog(@"Dia%d.jpg", i+1);
     NSString *imageFile = [[NSBundle mainBundle] pathForResource:tempString ofType:@"JPG"];
     BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imageFile];
  
     if (fileExists){
      [Displayimages addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:tempString ofType:@"JPG"]]];
      NSLog(@"img");
     }else {
      break;
     }
  
    }
  
  
    //NSArray * images = [NSArray arrayWithObjects:[UIImage imageNamed:@"1.jpg"], [UIImage imageNamed:@"2.jpg"], [UIImage imageNamed:@"3.jpg"], [UIImage imageNamed:@"4.jpg"], [UIImage imageNamed:@"5.jpg"], nil];
    //NSLog(@"Objects Img = %@", images);
    NSLog(@"Images %@",Displayimages);
    self.view = [[[SlideShowView alloc] initWithImages:Displayimages] autorelease];
    [Displayimages release];
   }
  
   return self;
  }
  
  // Override to allow orientations other than the default portrait orientation.
  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
      //return YES;
   return UIInterfaceOrientationIsLandscape (interfaceOrientation);
  
  }
  
  @end
  </code></pre>
  
  <p>If you would find a solution or tips, all are welcome !</p>
  
  <p>Thanks in advance </p>
  
  <p>kind regards Bart !</p>