I have 2 scroll views, and I need to load 106 Images into every scroll view, below is the code I’m using, but it crashes when I’m trying to test it on my iphone 4.
Every image is around 30-40KB, so the size is not the reason.
If you have any idea, I’ll be very thankful!
- (void)viewDidLoad
{
[super viewDidLoad];
Images = [[NSMutableArray alloc]init];
Slogans = [[NSMutableArray alloc]init];
for (int i =1; i<=106; i++) {
int r = i-1;
[self.Images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%da.jpg",i]]];
[self.Slogans addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]]];
NSLog(@"%d.jpg",i );
NSLog(@"%da.jpg",i );
NSLog(@"Images count: %d",self.Images.count);
NSLog(@"Slogans count: %d",self.Slogans.count);
//Images
CGRect frame;
frame.origin.x = self.ImagesScrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.ImagesScrollView.frame.size;
UIImageView *ImagesSubview = [[UIImageView alloc] initWithFrame:frame];
ImagesSubview.image = [self.Images objectAtIndex:r];
[self.ImagesScrollView addSubview:ImagesSubview];
[ImagesSubview release];
//Slogans
CGRect Sframe;
Sframe.origin.x = self.SlogansScrollView.frame.size.width * i;
Sframe.origin.y = 0;
Sframe.size = self.SlogansScrollView.frame.size;
UIImageView *SlogansSubview = [[UIImageView alloc] initWithFrame:Sframe];
SlogansSubview.image = [self.Slogans objectAtIndex:r];
[self.SlogansScrollView addSubview:SlogansSubview];
[SlogansSubview release];
}
[SpageControl setNumberOfPages:Images.count];
[pageControl setNumberOfPages:Images.count];
self.ImagesScrollView.contentSize = CGSizeMake(self.ImagesScrollView.frame.size.width * Images.count, self.ImagesScrollView.frame.size.height);
self.SlogansScrollView.contentSize = CGSizeMake(self.SlogansScrollView.frame.size.width * Slogans.count, self.SlogansScrollView.frame.size.height);
}
30-40 kb filesize indicates nothing about the actual memory needed to hold those images.
You would be better off using a UITableView and dynamically loading the images as needed OR using one of the UIScrollView examples from Apple: PhotoScroller and ScrollViewSutie which demo tiling.