【问题标题】:Core data images from desktop to iphone从桌面到 iPhone 的核心数据图像
【发布时间】:2023-04-01 04:40:01
【问题描述】:

我构建了一个简单的 mac 数据输入工具,用于 iPhone 应用程序。我最近添加了缩略图,我使用简单的绑定通过 Image Well 添加了该缩略图。它是一种可转换的数据类型,似乎工作正常。

但是,iPhone 应用程序不会显示图像。该属性不为空,但我无法显示图像。以下是针对 cellForRowAtIndexPath

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

NSManagedObject *entity = nil;
if ([self.searchDisplayController isActive])
    entity = [[self filteredListContent] objectAtIndex:[indexPath row]];
else
    entity = [fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [entity valueForKey:@"name"];
//cell.imageview.image = [UIImage imageNamed:@"ImageB.jpeg"]; //works fine
cell.imageView.image = [entity valueForKey:@"thumbnail"];//no error, but no file

return cell;

我认为问题出在可转换(我使用的是默认的 NSKeyedUnarchiveFromData),或者我如何调用缩略图。我是新手,非常感谢任何帮助。

【问题讨论】:

  • thumbnail 是什么类型的?是NSData吗?如果是这样,您将需要从该数据创建 UIImage。
  • 属性类型是可转换的(我的出发点是来自苹果的 Core Data Recipes 示例,它也使用了可转换的缩略图)。它使用自定义值转换器 (ImageToDataTransformer),由于 UIImage 的原因,该转换器无法在数据输入应用程序上编译。

标签: iphone core-data imageview nsmanagedobject


【解决方案1】:

听起来您将图像作为 NSImage 存储在桌面上,而 iPhone 上不存在该对象。您的桌面应用程序需要将图像存储在可移植的格式(PNG 或 JPG 等)中。然后您可以将其作为 UIImage 加载回您的 iPhone 应用程序中。

更新可变形

听起来您仍在将 NSImage 传递给属性,并且它认为您正在处理它的数据。您需要先将其转换为“标准”格式,如下所示:

NSBitmapImageRep *bits = [[myImage representations] objectAtIndex: 0];

NSData *data = [bits representationUsingType:NSPNGFileType properties:nil];
[myManagedObject setImage:data];

我建议编写自定义访问器来处理这个问题,如下所示:

#ifdef IPHONEOS_DEPLOYMENT_TARGET

- (void)setImage:(UIImage*)image
{
  [self willChangeValueForKey:@"image"];

  NSData *data = UIImagePNGRepresentation(image);
  [myManagedObject setImage:data];
  [self setPrimitiveValue:data forKey:@"image"];
  [self didChangeValueForKey:@"image"];
}

- (UIImage*)image
{
  [self willAccessValueForKey:@"image"];
  UIImage *image = [UIImage imageWithData:[self primitiveValueForKey:@"image"];
  [self didAccessValueForKey:@"image"];
  return image;
}

#else

- (void)setImage:(NSImage*)image
{
  [self willChangeValueForKey:@"image"];
  NSBitmapImageRep *bits = [[image representations] objectAtIndex: 0];

  NSData *data = [bits representationUsingType:NSPNGFileType properties:nil];
  [myManagedObject setImage:data];
  [self setPrimitiveValue:data forKey:@"image"];
  [self didChangeValueForKey:@"image"];
}

- (NSImage*)image
{
  [self willAccessValueForKey:@"image"];
  NSImage *image = [[NSImage alloc] initWithData:[self primitiveValueForKey:@"image"]];
  [self didAccessValueForKey:@"image"];
  return [image autorelease];
}

#endif

这将为您提供条件编译并将数据存储为可以在任何设备上检索的 NSData(PNG 格式)。

【讨论】:

  • 扎拉先生,我有你的书!感谢您的发表。我已遵循您的建议,但现在我无法保存我的数据输入文件(托管对象未保存,控制台消息“[NSImage length]: unrecognized selector sent to instance”)我已完成以下操作: - 添加像这里 tinyurl.com/ylk8d3m 的值转换器(肯定会被调用)-将此添加到 Image Well Value 绑定(不是您书中示例中的 ValuePath)-将其添加到数据模型可转换属性中谢谢您的帮助
  • 非常感谢,一旦我摆脱了价值转换器,这真是一种享受。
猜你喜欢
  • 2012-07-14
  • 2011-01-06
  • 1970-01-01
  • 1970-01-01
  • 2012-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多