在我的项目中,我将图像保存到手机上的文档文件夹中,然后我将它们加载到单独的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];
}
}
这是一个正在发生的事情的例子 :假设我拍了2张照片,一张叫“10282012_13113138_image.jpg”,另一张叫“10282012_13113468_image.jpg”。然后我去加载单元格中的图像,最后一张照片加载到两个单元格中。
非常感谢任何帮助!
答案 0 :(得分: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,你做同样的事情,并将其设置为数组中的最后一个图像,依此类推....