Loading images to UIScrollview crashes

Posted by Icky on Stack Overflow See other posts from Stack Overflow or by Icky
Published on 2010-06-14T05:13:57Z Indexed on 2010/06/14 5:22 UTC
Read the original article Hit count: 238

Filed under:
|
|
|
|

Hello All.

I have a Navigationcontroller pushing a UIViewController with a scrollview inside. Within the scrollview I download a certain number of images around 20 (sometimes more) each sized around 150 KB.

All these images are added to the scrollview so that their origin is x +imageSize and the following is sorted right to the one before.

All in all I think its a lot of data (3-4 MB). On an I pod Touch this sometimes crashes, the IPhone can handle it once, if it has to load the data again (some other images) , it crashes too.

I guess its a memory issue but within my code, I download the image, save it to a file on the phone as NSData, read it again from file and add it to a UIImageview which I release. So I have freed the memory I allocated, nevertheless it still crashes.

Can anyone help me out? Since Im new to this, I dont know the best way to handle the Images in a scrollview.

Besides I create the controller at start from nib, which means I dont have to release it, since I dont use alloc - right?

Code:

In my rootviewcontroller I do:

-(void) showImages {



 [[self naviController] pushViewController:imagesViewController animated:YES];
 [imagesViewController viewWillAppear:YES];


}

Then in my Controller handling the scroll View, this is the method to load the images:

- (void) loadOldImageData {


for (int i = 0; i < 40 ; i++) {


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString  stringWithFormat:@"img%d.jpg", i]];





NSData *myImg = [NSData dataWithContentsOfFile:filePath];
UIImage *im = [UIImage imageWithData:myImg];


if([im isKindOfClass:[UIImage class]]) {
 NSLog(@"IM EXISTS");

 UIImageView *imgView = [[UIImageView alloc] initWithImage:im];


CGRect frame = CGRectMake(i*320, 0, 320, 416);
imgView.frame = frame;

[myScrollView addSubview:imgView];
[imgView release];



 //NSLog(@"Adding img %d", i);
numberImages = i;
 NSLog(@"setting numberofimages to %d", numberImages);
//NSLog(@"scroll subviews %d", [myScrollView.subviews count]);

}

}


myScrollView.contentSize = CGSizeMake(320 * (numberImages + 1), 416);


 }

© Stack Overflow or respective owner

Related posts about iphone

Related posts about memory