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

use*_*434 10 iphone core-data imageview nsmanagedobject

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

但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;
Run Code Online (Sandbox Code Playgroud)

我正在考虑问题是可转换的(我使用的是默认的NSKeyedUnarchiveFromData),或者我是如何调用缩略图的.我是新手,所以任何帮助都将不胜感激.

Mar*_*rra 15

听起来像是在桌面上将图像存储为NSImage,而iPhone上不存在该对象.您的桌面应用程序需要将图像存储在便携式,PNG或JPG等内容中.然后您就可以将其作为UIImage加载回iPhone应用程序.

更新可重新转换

听起来你仍然在NSImage中传递属性,它认为你正在处理它的数据.您需要先将其转换为"标准"格式,如下所示:

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

NSData *data = [bits representationUsingType:NSPNGFileType properties:nil];
[myManagedObject setImage:data];
Run Code Online (Sandbox Code Playgroud)

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

#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
Run Code Online (Sandbox Code Playgroud)

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