0

在我的项目中,我将图像保存到手机上的文档文件夹中,然后将它们加载到单独的 tableview 中。我取得了一些成功,拍摄的最后一张图像被加载到表中,但被加载到每一行而不是最后一个。这是我用来在 tableview 中加载图像的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"List";
ListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//Load PLIST
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [path objectAtIndex:0];
NSString *plistPath = [NSString stringWithFormat:@"%@/images.plist", documentsDirectory];
//Load PLIST into mutable array
NSMutableArray *imageArray = [NSMutableArray arrayWithContentsOfFile:plistPath];

for (NSDictionary *dict in imageArray) {
//Do whatever you want here, to load an image for example
   NSString *imageFilePath = [NSHomeDirectory() stringByAppendingPathComponent:[dict objectForKey:@"Original Image"]];
   UIImage *image = [UIImage imageWithContentsOfFile:imageFilePath];
   [cell.imageofItem setImage:image];
}   
}

这是正在发生的事情的一个例子:假设我拍了两张照片,一张叫做“10282012_13113138_image.jpg”,另一张叫做“10282012_13113468_image.jpg”。然后我去加载单元格中的图像,最后一张照片加载到两个单元格中。

任何帮助将非常感激!

4

1 回答 1

1

代替

for (NSDictionary *dict in imageArray) {
   NSString *imageFilePath = [NSHomeDirectory() stringByAppendingPathComponent:[dict objectForKey:@"Original Image"]];
   UIImage *image = [UIImage imageWithContentsOfFile:imageFilePath];
   [cell.imageofItem setImage:image];

}

尝试

   NSDictionary *dict = [imageArray objectAtIndex:indexPath.row];
   NSString *imageFilePath = [NSHomeDirectory() stringByAppendingPathComponent:[dict objectForKey:@"Original Image"]];
   UIImage *image = [UIImage imageWithContentsOfFile:imageFilePath];
   [cell.imageofItem setImage:image];

问题在于,对于每个 indexPath.row,您都在迭代直到数组中的最后一个元素,不断覆盖单元格图像,直到到达最后一个图像。然后对于下一个 indexPath.row,您执行相同的操作,并将其设置为数组中的最后一个图像,依此类推......

于 2012-10-28T03:13:32.953 回答