IOS 下载图片、tableViewCell 和 Array

IOS download image, tableViewCell and Array

我有一个数组。 在我的数组中有 8 个对象序列化:

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

我已经下载了所有图片并保存到文件夹中。 现在,如何从文档文件夹中获取所有图像并在 tableViewCell 中显示同一个 myArray?

我使用此代码从文档文件夹中获取所有图像。

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];

但是,我无法在 myArray 中同时显示序列化的文本和图像。

如您所说,您可以在以下行之后将图像名称放入 onlyImages 数组中:

NSArray *onlyImages = [directoryContents filteredArrayUsingPredicate:filter];

所以让我们用它来获取和创建 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
}

希望对您有所帮助!

干杯!

-(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];
    }