0

The following is the code I am using to display image in the table view and its name. It works fine however when we have lot of images inside the folder the app crashes. Any help is greatly appreciated.

NSString *CellIdentifier = @"DocumentList";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:CellIdentifier] autorelease];

}

   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *bundleRoot = [paths objectAtIndex:0];

    NSString *dataPath = [bundleRoot stringByAppendingPathComponent:[NSString stringWithFormat:@"%d", 1]];
    NSString *imagePath = [NSString stringWithFormat:@"%@/%@", dataPath, [itsDocumentNamesArray objectAtIndex:indexPath.row]];

    [cell.imageView setImage:[[[UIImage alloc] initWithContentsOfFile:imagePath] autorelease]];
    cell.textLabel.text = [itsDocumentNamesArray objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:15.0];
    cell.textLabel.textColor = [UIColor grayColor];
Dilip Rajkumar
  • 7,006
  • 6
  • 60
  • 76
  • hope this will help you http://stackoverflow.com/questions/1130089/lazy-load-images-in-uitableview check the memory leak in your code too.. – Marios Oct 12 '11 at 11:22
  • I saw this code https://github.com/slavingia/SLGlobalImageCache even if I store image in cache when there is more no of image I think it will crash. Because it crashes when loading it first time itself. – Dilip Rajkumar Oct 12 '11 at 12:17
  • okay where is your images are stores – Marios Oct 12 '11 at 12:45
  • my images are in my documents directory..I have folders inside my documents directory as numbers so I will access one directory and display all the images iside that directory along with its tame... – Dilip Rajkumar Oct 12 '11 at 13:02
  • use pojo class for loading the image, it wont crash – Marios Oct 12 '11 at 13:07
  • i think this link help u http://developer.apple.com/iphone/library/samplecode/LazyTableImages/index.html – Rahul Juyal Oct 12 '11 at 11:56
  • Thank you for your suggestion I saw lazytableimages it is used for getting images from web. My app crashes when loading that view itself. If I have less no of images in that folder my code working perfect. Can you post some working code that is getting image from application Directory. – Dilip Rajkumar Oct 12 '11 at 12:13
  • @Dilip instead of [[[UIImage alloc] initWithContentsOfFile:imagePath] autorelease]]; try using [UIImage imageNamed:imagePath]; Hope this helps... – stack2012 Oct 20 '11 at 04:18
  • Thank you booleanBoy. I will try it and let you know in few hours.. – Dilip Rajkumar Oct 20 '11 at 08:17
  • I tried imageNames but the image is not displayed inside tableview cell.. – Dilip Rajkumar Oct 20 '11 at 08:29
  • try the below answer...I set it as a subview to contentview and it worked fyn for me.... – stack2012 Oct 20 '11 at 09:22

1 Answers1

0
    UIImageView *temp=[[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 60, 60)]; //40 
    temp.clipsToBounds=YES;
    temp.tag=10;
    temp.userInteractionEnabled=YES;
    temp.layer.cornerRadius=8.0;
    [cell.contentView addSubview:temp];
    [temp release];

Have the above part within the cell==nil block and the following piece of code outside...

    UIImageView *temp=(UIImageView*)[cell.contentView viewWithTag:10];
    NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    NSString *docDirectory = [sysPaths objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", docDirectory,[tableData objectAtIndex:indexPath.row]];
    UIImage *cellImage=[UIImage imageWithContentsOfFile:filePath];
    temp.image=cellImage;
stack2012
  • 2,146
  • 2
  • 16
  • 23
  • Doing the imageView allocation and adding it as a subview within cell==nil block will save memory due to cell reuse.... And we are just assigning an image to that imageView again and again...I think this could avoid crashing of app in ur case when u load more images...Happy coding... – stack2012 Oct 20 '11 at 09:29
  • Thank you very much for your kind reply I will put the code and update you in few min... – Dilip Rajkumar Oct 20 '11 at 09:39
  • Still it is not working its crashing in my mobile. I got 8 images inside one folder. Or is it possible to reduce the size of the image then paint table will that save me... – Dilip Rajkumar Oct 20 '11 at 10:24
  • Sorry I did not notice your answer. I debugged I was able to debug cellForRowAtIndexPath method fully then it weights for some time then my app closes with no clue....Even no EXE_BAD_ACCESS – Dilip Rajkumar Oct 20 '11 at 13:34