Võ Mai Trinh
Võ Mai Trinh

Reputation: 55

IOS download image, tableViewCell and Array

I have an Array. Inside my array have 8 object seriated:

 - text
 - text
 - link Url Image.
 - link Url Image.
 - text
 - link Url Image.
 - link Url Image.
 - text

I had downloaded all image and save to document folder. And now, How can I get all image from document folder and show in tableViewCell seriated same myArray?

I used this code to get all image from document folder.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *myPath = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *directoryContents = [fileManager contentsOfDirectoryAtPath:myPath error:nil];
NSMutableArray *subpredicates = [NSMutableArray array];
            [_arrMessageFree addObject:[NSPredicate predicateWithFormat:@"SELF ENDSWITH '.png'"]];
            [_ar addObject:[NSPredicate predicateWithFormat:@"SELF ENDSWITH '.jpg'"]];
NSPredicate *filter = [NSCompoundPredicate orPredicateWithSubpredicates:subpredicates];

NSArray *onlyImages = [directoryContents filteredArrayUsingPredicate:filter];

But, I cannot show both text and image seriated in myArray.

Upvotes: 0

Views: 107

Answers (2)

Yagnesh Dobariya
Yagnesh Dobariya

Reputation: 2251

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

//after cell initialization add following code

         for (UIView *subview in [cell.contentView subviews]) 
          {
                     [subview removeFromSuperview];
          }
          UIImage *img=[UIImage imageWithContentsOfFile:[onlyImages objectAtIndex:indextPath.row]];
          [cell.contentView addSubview:img];
    }

Upvotes: 1

Randika Vishman
Randika Vishman

Reputation: 8124

As you have told, you are able to get the image names into onlyImages array after the following line:

NSArray *onlyImages = [directoryContents filteredArrayUsingPredicate:filter];

So let's use it to fetch and create UIImages:

UIImage *temp = nil;
for (int i = 0; i < [onlyImages count]; i++) {
   temp = [UIImage imageNamed:[onlyImages objectAtIndex:i]]; // so now you have your image loaded into `temp` UIImage object
}

Hope this was helpful!

Cheers!

Upvotes: 0

Related Questions