开发者

Xcode: UIScrollView image gallery, having memory problems

I'm Trying to develop an UIScrollView Based Image gallery.

So let me explain what i'm trying to achieve here:

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 UIScrollview which has 3 UIImageViews which you create and remove from superview.

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, sta开发者_运维百科rt up the application and run it.

after i viewed about 50-70 slides the app crashes ( out of memory). My knowledge of Obj. C isn't that great .

You'll find the code below: it Prob. has something to do with releasing the images. Improvements to the code would be really helpful

#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 >= [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 > 0)
  {
   mCurrentImageView = [self createImageView:0];
   [self addSubview:mCurrentImageView];

   if (imageCount > 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 > 0 && swipeDistance > 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 > 0)
  {
   mLeftImageView = [self createImageView:mCurrentImage - 1];
   mLeftImageView.hidden = YES;

   [self addSubview:mLeftImageView];
  }
  else
  {
   mLeftImageView = nil;
  }
 }
 else if (mCurrentImage < count - 1 && swipeDistance < -50.0f)
 {
  mLeftImageView.image = nil;
  //[mLeftImageView.image release];
  [mLeftImageView removeFromSuperview];
  [mLeftImageView release];
  //mLeftImageView = nil;

  mLeftImageView = mCurrentImageView;
  mCurrentImageView = mRightImageView;

  mCurrentImage++;
  if (mCurrentImage < 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<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

If you would find a solution or tips, all are welcome !

Thanks in advance

kind regards Bart !


You definitely have a memory leak in createImageView:. You will have to change

return result;

to

return [result autorelease];

There might be more leaks, but that's an obvious one.

  • Johannes
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜