我想显示一张从核心数据中检索到的图像,每个单元格都有不同的图像。我已经这样做了,但我不知道如何从此路径中检索并将图像放入单元格中。任何人都可以帮助我吗?提前致谢。
NSString * filePath = @"01.jpg";
NSData * imageData = UIImageJPEGRepresentation([UIImage imageNamed:filePath], 0.1);
[productInfo setValue:imageData forKey:@"productImage"];
最佳答案
您可以从您的模型中获取托管对象并将其放置在数组中,该数组将包含如下所示的照片名称。我假设您的实体是 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];
这就是您将存储图像名称添加到核心数据中,然后检索名称构建路径并显示它的方式。
关于objective-c - 从 CoreData 检索到的 TableView 上的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5972827/