gpt4 book ai didi

iphone - 核心数据图像从桌面到iPhone

转载 作者:行者123 更新时间:2023-12-03 18:30:26 25 4
gpt4 key购买 nike

我构建了一个简单的 Mac 数据输入工具,与 iPhone 应用程序一起使用。我最近添加了缩略图,这是我使用简单的绑定(bind)通过图像井添加的。它是一种可转换的数据类型,似乎工作得很好。

但是 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),要么是我如何调用缩略图。我是新手,因此我们将不胜感激。

最佳答案

听起来您正在将图像作为 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 格式),可以在任何设备上检索。

关于iphone - 核心数据图像从桌面到iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2271195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com