【问题标题】:Display NSImage from NSData从 NSData 显示 NSImage
【发布时间】:2011-03-14 13:47:23
【问题描述】:

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

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

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

现在,如果我使用以下命令初始化 imageView:

NSImage *imageView = [[NSImage alloc] initWithContentsOfFile:path];

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

如果我尝试:

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

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

惠特 CGImageRef:

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

图像仍然渲染得太小。

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

【问题讨论】:

  • 如果initWithContentsOfFile: 适用于NSImage,为什么还需要NSData? (但我同意这很奇怪——可能是图像表示或尺寸问题。)
  • 我已经编辑了我的帖子:添加了 imageSource
  • 我有一个用于读取 exif 标签的 NSData。
  • @Joe Blow:我不认为改变 dpi 可能是解决方案。原始分辨率约为2800x1300,结果约为100x70。
  • 我想我只得到一个拇指而不是完整的图像..但我不知道如何检索存储真实图像的 NSData 部分。

标签: objective-c nsview nsdata nsimage cgimage


【解决方案1】:
NSImage *imageView = [[NSImage alloc] initWithData:(NSData *)imageData];

【讨论】:

  • 感谢您的回复。我会尽快尝试,我会反馈给你的。
  • 对不起,如果我回复你晚了。当我将您的代码粘贴到我的项目中时,编译器会显示 error: Semantic Issue: Unknown receiver 'UIImage';您的意思是“CIImage”吗? 如果我将 UIImage 更改为 CIImage,编译器会告诉我警告:语义问题:不兼容的指针类型使用“CIImage *”类型的表达式初始化“NSImage *”如果我尝试运行编译器说 Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_CIImage", 引用自: ButtonsAppDelegate.old 中的 objc-class-ref: symbol(s) not found for architecture x86_64 clang: error:链接器命令失败,退出代码为 1
  • 将 UIImage 更改为 NSImage。他可能是无意中输入的。
  • NSImage 不存在 imageWithData 方法。
  • 代码示例已更正,有错字,确实应该是 initWithData: not imageWithData:
猜你喜欢
  • 2011-08-16
  • 2011-08-11
  • 1970-01-01
  • 2012-07-28
  • 1970-01-01
  • 2014-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多