views:

268

answers:

2

I am trying to display about 53 images in Xcode for iphone, but about the 37th image crashes my entire app! If any one sees any errors in my code, I would really appreciate your help. Thank you!!

I think I am not releasing my images somewhere...just not sure what to do!

#import "MyProjectViewController.h"

@implementation MyProjectViewController


@synthesize scrollView1;

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}


const CGFloat kScrollObjHeight = 320.0;
const CGFloat kScrollObjWidth = 480.0;
const NSUInteger kNumImages = 53;


- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;

curXLoc += (kScrollObjWidth);
}
}

[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}

- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];

[scrollView1 setBackgroundColor:[UIColor blackColor]];
[scrollView1 setCanCancelContentTouches:NO];
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.clipsToBounds = YES;
scrollView1.scrollEnabled = YES;
scrollView1.pagingEnabled = YES;




NSUInteger i;

for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:@"c1_%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i;
[scrollView1 addSubview:imageView];
[imageView release];

}

[self layoutScrollImages];

}



- (void)dealloc
{
[scrollView1 release];

[super dealloc];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}


@end

Thank you!!!!!

+1  A: 

How big are images? I suspect the problem is not with code itself, but with memory limitations of iPhone/iPod - if you're loading big images they simply eat all memory and program is closed.

Valerii Hiora
A: 

Well the most likely reason is that you run out of memory. Put an NSLog message in didReceiveMemoryWarning to check if you are getting a warning for low memory.

In any case I would suggest that you lazy load images and not all of the images at once. It will greatly reduce to load time of your app and will also probably solve your memory problems. Use the UIScrollViewDelegate to know when to load images that weren't loaded yet according to the position of the UIScrollView offset value.

Ron Srebro