从桌面到 iPhone 的核心数据图像 [英] Core data images from desktop to iphone

查看:17
本文介绍了从桌面到 iPhone 的核心数据图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I built a simple mac data entry tool I use with an iPhone application. I've recently added thumbnail which I added via an Image Well using simple bindings. Its a transformable data type which seems to work fine.

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

The iPhone application however won't show the images. The attribute isn't null but I can't get an image to appear. The following is for 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),要么是我调用缩略图的方式.我是一个新手,所以任何帮助将不胜感激.

I'm thinking either the problem is with the transformable (I'm using the default NSKeyedUnarchiveFromData), or how I'm calling the thumbnail. I'm a newbie so any help would be greatly appreciated.

推荐答案

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

Sounds like you are storing the image as an NSImage on the desktop and that object does not exist on the iPhone. Your desktop app needs to store the image in something that is portable, a PNG or JPG, etc. Then you will be able to load it back into your iPhone application as a UIImage.

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

Sounds like you are still passing in a NSImage to the attribute and it is thinking you are handling it data. You need to convert it to a "standard" format first, like this:

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

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

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

I recommend writing custom accessors to handle this, like the following:

#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 格式).

This will give you a conditional compile and will store the data as NSData (in PNG format) that can be retrieved on any device.

这篇关于从桌面到 iPhone 的核心数据图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆