aarti Garg
aarti Garg

Reputation: 161

Loading images on UIScroll from documents directory

I have different images for portrait and landscape, I fetch the images from the Document directory, their is a total of 62 images. 31 Landscape images having 1015x745 dimensions and 31 Portrait having 751x1024 dimensions. All the images loading on the simulator but when i run the same code within the device the application crash. It load either only the portrait on landscape images.

int porWidth = 768, lanWidth = 1024;
int i=0;
    for (CatalogIndividuals *cat in self.array)
    {
        UIImageView *imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(porWidth*i +5, 0, 757, 964)] autorelease];
        imageView1.tag = porImage+i;

        NSString *fullPath =[self.documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",cat.portraitImage]];

        imageView1.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:[fullPath stringByReplacingOccurrencesOfString:@"\\" withString:@"/"]]];
        imageView1.contentMode = UIViewContentModeScaleAspectFit;

        UIImageView *imageView2 = [[[UIImageView alloc] initWithFrame:CGRectMake(lanWidth*i +5, 0, 1015, 620)] autorelease];
        imageView2.tag = lanImage+i;

        NSString *tempPath = [self.documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",cat.landscapImage]];
        NSString *str = [tempPath stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];

        NSLog(@"imageView1 fullPath = %@",str);
        imageView2.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:str]];}

I think there is some memory leak problem. please help if anybody has any solution.

Upvotes: 0

Views: 178

Answers (1)

Jim
Jim

Reputation: 73936

That's about 350MB worth of images. You don't have a memory leak, you're just using far too much memory. This is a mobile device, not a desktop computer, the resources available to you are limited.

You shouldn't be loading all the images at once, you should be loading them dynamically as you scroll through the content. Take a look at the sample code Apple provides for UIScrollView to see how you can use tiling to achieve this.

Upvotes: 2

Related Questions