从CoreData检索的TableView上的图像

时间:2011-05-12 02:51:26

标签: objective-c ios uitableview core-data

我想显示从核心数据中检索的图像,并且每个单元格都会有不同的图像。我已经这样做但我不知道如何从这条路径中检索并将图像放入单元格。有人可以帮我吗?提前谢谢。

 NSString * filePath = @"01.jpg";
NSData * imageData = UIImageJPEGRepresentation([UIImage imageNamed:filePath], 0.1);
[productInfo setValue:imageData forKey:@"productImage"];

2 个答案:

答案 0 :(得分:1)

您可以从模型中获取托管对象,并将其放置在数组中,其中数组将包含照片名称,如下所示。我假设您的实体是 photoStore ,用于存储照片文件名称的列是 nameOfPhoto ,因此代码看起来像,

NSFetchRequest *fetchRequest=[[[NSFetchRequest alloc] init] autorelease]; 
NSEntityDescription *entity=[NSEntityDesription entityForName:@"photoStore" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity : entity];
NSArray fecthedObject = [fetchRequest executeFetchRequest:fetchRequest error:nil];
for(NSManagedOect *photos in fetchedObect)
{
  [self.arrayOfImageNames addObject : [photos valueForKey :@nameOfPhoto"]];
}

现在,在cellForRowAtIndexPath中使用图像名称将图像添加到单元格的imageview中,

NSString *imagePath = [NSHomeDirectory() stringByAppendingString : [[self.arrayOfImageNames objectAtIndex:indexpath.row] stringByAppendingPathComponent:"jpg"]];
cell.imageView.image=[UIImage imageNamed:imagePath];

这是你将图像名称存储到核心数据中然后检索名称构建它的路径并显示它的方法。

答案 1 :(得分:0)

如果你想让路径使用这个 -

[[NSBundle mainBundle] pathForResource:@"01" ofType:@"jpg"];