如何从tableView中的目录获取图像

时间:2014-02-05 07:42:12

标签: iphone

我从文档目录中获取图像。这是成功的getiing。但当它在桌子上加载时,它会在桌面上显示相同的图像。

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

1 个答案:

答案 0 :(得分: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];