2014-02-05 21 views
0

我從文檔目錄中獲取圖像。它成功地getiing。但是當它在桌面上加載時,它會在桌面上出現相同的圖像。如何在tableView中獲取圖像表格目錄

arrayOfImages = [[NSMutableArray alloc]init]; 

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

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

//的NSString * stringPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);在cellForRowAtIndexPath方法

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); 
     } 
    } 

} 

對於表I M調用此代碼

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

回答

1

試試這個。

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]; 
相關問題