【问题标题】:Image on TableView which is retrieved from CoreData从 CoreData 检索的 TableView 上的图像
【发布时间】:2011-05-12 02:51:25
【问题描述】:

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

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

【问题讨论】:

    标签: objective-c ios uitableview core-data


    【解决方案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];
    

    这就是您如何将图像名称添加到核心数据中,然后检索名称构造一个路径并显示它。

    【讨论】:

      【解决方案2】:

      如果你想获取路径,请使用这个 -

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

      【讨论】:

      • 我想从核心数据中检索图像,然后将其放入 UITableViewCell。
      猜你喜欢
      • 2017-01-14
      • 1970-01-01
      • 2020-01-02
      • 2016-10-24
      • 1970-01-01
      • 2018-06-05
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      相关资源
      最近更新 更多