从NSData显示NSImage

时间:2011-03-14 13:47:23

标签: objective-c nsview nsdata nsimage cgimage

我的目标是在视图中显示图像。 考虑到我有:

  • IBOutlet NSImageView *图像用于显示我的图像
  • 用于读取图像文件的NSData * imageData
  • NSImage * imageView

在imageData中存储图像(我使用了initWithContentsOfFile方法)。

现在,如果我使用:

初始化imageView
NSImage *imageView = [[NSImage alloc] initWithContentsOfFile:path];

我可以正确地看到图像的渲染,但是这样我就从文件系统中读取了两次。

如果我尝试:

NSImage *imageView = [[NSImage alloc] initWithData:imageData];

显示的图像非常小..像拇指

Whit CGImageRef:

CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
CGImageRef imageRef= CGImageSourceCreateImageAtIndex(imageSource, 0, nil);
NSImage *imageView = [[NSImage alloc] initWithCGImage:imageRef size:NSZeroSize];

图像仍然渲染得太小。

如何以原始分辨率显示图像?

1 个答案:

答案 0 :(得分:5)

NSImage *imageView = [[NSImage alloc] initWithData:(NSData *)imageData];