Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从桌面到iphone的核心数据图像_Iphone_Core Data_Imageview_Nsmanagedobject - Fatal编程技术网

从桌面到iphone的核心数据图像

从桌面到iphone的核心数据图像,iphone,core-data,imageview,nsmanagedobject,Iphone,Core Data,Imageview,Nsmanagedobject,我构建了一个简单的mac数据输入工具,用于iPhone应用程序。我最近添加了缩略图,我使用简单的绑定通过图像井添加了缩略图。它是一种可转换的数据类型,似乎工作正常 然而,iPhone应用程序不会显示这些图像。属性不是null,但我无法获得要显示的图像。以下是cellForRowAtIndexPath的示例 static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCe

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

然而,iPhone应用程序不会显示这些图像。属性不是null,但我无法获得要显示的图像。以下是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),要么在于如何调用缩略图。我是一名新手,因此非常感谢您的帮助。

听起来您将图像存储为桌面上的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格式),可在任何设备上检索。

Zarra先生,我有您的书!谢谢你的发帖。我已经按照你的建议做了,但是现在我无法保存我的数据输入文件(托管对象不保存,控制台消息为“[NSImage length]:未识别的选择器发送到实例”),我已经做了以下工作:-添加了像这里这样的值转换器(它们肯定被调用)-将其添加到图像值绑定中(与本书示例中的ValuePath不同)-将其添加到数据模型可转换属性中感谢您的帮助非常感谢,这在我摆脱值转换器后起到了很大的作用。缩略图是什么类型的?
是否为NSData
?如果是这样,您需要从该数据创建UIImage。属性类型是可转换的(我的出发点是苹果公司的Core Data Recipes样本,它也使用了一个可转换的缩略图)。它使用了一个自定义值转换器(ImageToDataTransformer),由于UIImage,它无法在数据输入应用程序上编译。