我正在努力解决在NSView. 当我第一次从文件中加载 eps 文件并绘制它时drawInRect:,图像会正确显示。但是,当我从存档文件加载图像时,不会绘制图像。
我准备了一个肮脏的小例子,你可以复制/粘贴并试用。创建一个新的 Cocoa App 项目并将其添加到委托方法中。
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
  // Just a sample eps file
  NSURL *url = [NSURL URLWithString: @"http://embedded.eecs.berkeley.edu/concurrency/latex/figure.eps"];
  NSImage *epsImage = [[[NSImage alloc] initWithContentsOfURL: url] autorelease];
  // Encode data
  NSMutableData *mutableData = [[[NSMutableData alloc] init] autorelease];
  NSKeyedArchiver *coder = [[[NSKeyedArchiver alloc] initForWritingWithMutableData: mutableData] autorelease];
  [coder encodeObject: epsImage forKey: @"image"];
  [coder finishEncoding];
  NSString *dataFile = [@"~/Desktop/image.data" stringByExpandingTildeInPath];
  [mutableData writeToFile: dataFile atomically: YES];
  // Decode data
  NSData *data = [NSData dataWithContentsOfFile: dataFile];
  NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData: data];
  NSImage *loadedImage = [decoder decodeObjectForKey: @"image"];
  // Draw image
  NSRect rect;
  rect.origin = NSZeroPoint;
  rect.size = loadedImage.size;
  NSView *view = [[NSApp mainWindow] contentView];
  [view lockFocus];
  [loadedImage drawInRect: rect fromRect: rect operation: NSCompositeSourceOver fraction: 1.0];
  [view unlockFocus];
}
为了证明第一个加载的图像绘制正确,只需将行更改[loadedImage drawInRect:...]为[epsImage drawInRect:...].
我在这里使用NSKeyedArchiverandNSKeyedUnarchiver来模拟encodeWithCoder:and initWithCoder:。因此,请关注这样一个事实NSImage,NSEPSImageRep即不包含预览(来自资源分支?)并且纯粹作为 eps 命令加载的表示未NSView正确绘制在 a 上。
任何帮助表示赞赏。