【问题标题】:How to get images form directory in tableView如何从 tableView 中的目录中获取图像
【发布时间】:2014-02-05 07:42:12
【问题描述】:

我正在从文档目录中获取图像。它成功地得到了。但是当它加载到桌子上时,它会在桌子上出现相同的图像。

arrayOfImages = [[NSMutableArray alloc]init];

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

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

// NSString *stringPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSLog(@"%@",paths);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"%@",documentsDirectory);

filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
NSLog(@"files array %@", filePathsArray);


for(i=0;i<[filePathsArray count];i++)
{
    NSString *strFilePath = [filePathsArray objectAtIndex:i];
    NSLog(@"%@",strFilePath);
    if ([[strFilePath pathExtension] isEqualToString:@"JPG"] || [[strFilePath pathExtension] isEqualToString:@"png"] || [[strFilePath pathExtension] isEqualToString:@"PNG"])
    {
        NSString *imagePath = [[stringPath stringByAppendingFormat:@"/"] stringByAppendingFormat:strFilePath];
        NSLog(@"%@",imagePath);

        NSData *data = [NSData dataWithContentsOfFile:imagePath];

        if(data)
        {
            UIImage *image = [UIImage imageWithData:data];
            NSLog(@"%@",image);
            [arrayOfImages addObject:image];
            NSLog(@"%@",arrayOfImages);
        }
    }

}

对于表我调用此代码

   saveImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10.0, 10.0, 100.0, 80.0)];
    [saveImageView setImage:[arrayOfImages objectAtIndex:indexPath.row]];
[cell.contentView addSubview:saveImageView];

【问题讨论】:

    标签: iphone


    【解决方案1】:

    在你的cellForRowAtIndexPath 方法中试试这个。

    static NSString *CellIdentifier = @"Cell"; 
    
    UIImageView *saveImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10.0, 10.0, 100.0, 80.0)];
    
    if (cell == nil)
    {
    
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; // You cell initialization
             saveImageView.tag = 1000;
         [cell.contentView addSubview:saveImageView];
    }
    
    ((UIImageView *)[cell.contentView viewWithTag:1000]).image = [arrayOfImages objectAtIndex:indexPath.row];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-28
      • 2020-10-24
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多