尝试将 [NSImage imageNamed:NSImageNameUser] 转换为 NSData
如果我通过类似的方式创建一个 NSImage
:
NSImage *icon = [NSImage imageNamed:NSImageNameUser];
它只有一种表示,一个 NSCoreUIImageRep
,它似乎是一个私有类。
我想将此图像存档为 NSData
但如果我要求 TIFFRepresentation
我会得到一个 当我最初创建的真实 NSImage 似乎是矢量并且会放大以很好地填充我的图像视图时,小图标。
我有点希望以这种方式制作的图像有一个我可以使用的 NSPDFImageRep 。
有什么想法如何获得此 NSImage
的 NSData
(首选矢量版本或更糟糕的是大规模位图版本)?
更新
在 Twitter 上与一些人交谈,他们建议真正的来源这些图像是多分辨率 icns 文件(可能根本不是矢量)。我无法在磁盘上找到它们的位置,但仍然很有趣。
此外,他们建议我创建系统 NSImage 并手动将其渲染为我自己的高分辨率 NSImage。我现在正在这样做,它可以满足我的需求。我的代码:(
+ (NSImage *)pt_businessDefaultIcon
{
// Draws NSImageNameUser into a rendered bitmap.
// We do this because trying to create an NSData from
// [NSImage imageNamed:NSImageNameUser] directly results in a 32x32 image.
NSImage *icon = [NSImage imageNamed:NSImageNameUser];
NSImage *renderedIcon = [[NSImage alloc] initWithSize:CGSizeMake(PTAdditionsBusinessDefaultIconSize, PTAdditionsBusinessDefaultIconSize)];
[renderedIcon lockFocus];
NSRect inRect = NSMakeRect(0, 0, PTAdditionsBusinessDefaultIconSize, PTAdditionsBusinessDefaultIconSize);
NSRect fromRect = NSMakeRect(0, 0, icon.size.width, icon.size.width);;
[icon drawInRect:inRect fromRect:fromRect operation:NSCompositeCopy fraction:1.0];
[renderedIcon unlockFocus];
return renderedIcon;
}
试图将此作为我的答案发布,但我没有足够的声誉?)
If I create an NSImage
via something like:
NSImage *icon = [NSImage imageNamed:NSImageNameUser];
it only has one representation, a NSCoreUIImageRep
which seems to be a private class.
I'd like to archive this image as an NSData
but if I ask for the TIFFRepresentation
I get a
small icon when the real NSImage
I originally created seemed to be vector and would scale up to fill my image views nicely.
I was kinda hoping images made this way would have a NSPDFImageRep
I could use.
Any ideas how can I get an NSData
(pref the vector version or at worse a large scale bitmap version) of this NSImage
?
UPDATE
Spoke with some people on Twitter and they suggested that the real source of these images are multi resolution icns files (probably not vector at all). I couldn't find the location of these on disk but interesting to hear none-the-less.
Additionally they suggested I create the system NSImage and manually render it into a high res NSImage of my own. I'm doing this now and it's working for my needs. My code:
+ (NSImage *)pt_businessDefaultIcon
{
// Draws NSImageNameUser into a rendered bitmap.
// We do this because trying to create an NSData from
// [NSImage imageNamed:NSImageNameUser] directly results in a 32x32 image.
NSImage *icon = [NSImage imageNamed:NSImageNameUser];
NSImage *renderedIcon = [[NSImage alloc] initWithSize:CGSizeMake(PTAdditionsBusinessDefaultIconSize, PTAdditionsBusinessDefaultIconSize)];
[renderedIcon lockFocus];
NSRect inRect = NSMakeRect(0, 0, PTAdditionsBusinessDefaultIconSize, PTAdditionsBusinessDefaultIconSize);
NSRect fromRect = NSMakeRect(0, 0, icon.size.width, icon.size.width);;
[icon drawInRect:inRect fromRect:fromRect operation:NSCompositeCopy fraction:1.0];
[renderedIcon unlockFocus];
return renderedIcon;
}
(Tried to post this as my answer but I don't have enough reputation?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的两个主要问题都在 Cocoa 绘图指南(从NSImage API 参考)有一个图像部分,您确实需要仔细阅读并在遇到代表/缓存/大小调整/质量问题时参考。
该问题的图像部分的相关小节是:如何选择图像表示、图像和缓存以及图像大小和分辨率。默认情况下,TIFF 图像的 -cacheMode 的行为就像 NSImageCacheBySize 设置生效一样。此外,对于内存中的缩放/大小调整操作,-imageInterpolation 也很重要:“表 6-4 列出了可用的插值设置。”和“NSImageInterpolationHigh - 更慢,更高质量的插值。”
我相当确定这适用于命名系统映像以及任何其他映像。
相关小节:图像表示。 “...使用基于文件的图像,您创建的大多数图像只需要单个图像表示。”但是,“您可能会在以下情况下创建多个表示形式:对于打印,您可能需要创建图像的 PDF 表示形式或高分辨率位图。”
您将获得适合加载图像的表示形式。例如,您必须为 TIFF 图像创建 PDF 表示形式。要以高分辨率执行此操作,您需要返回缓存模式,以便获得更高分辨率的项目。
由于图像/创建机制/设置/以及您想要用它们做什么的大量排列,有太多的细节无法列出。我的帖子旨在提供一般指南,帮助您找到适合您的情况所需的具体信息。
有关更多详细信息,请添加具体细节:您尝试使用的代码、您正在加载或创建的图像类型 - 您似乎在第四段中提到了两种不同的可能性 - 以及出了什么问题。
Both of your major questions are answered in the Cocoa Drawing Guide (companion guide linked from the NSImage API reference) has an Images section you really need to read thoroughly and refer to any time you have rep/caching/sizing/quality issues.
Relevant subsections of the Images section for this question are: How an Image Representation is Chosen, Images and Caching, and Image Size and Resolution. By default, the -cacheMode for a TIFF image "Behaves as if the NSImageCacheBySize setting were in effect." Also, for in-memory scaling/sizing operations, -imageInterpolation is important: "Table 6-4 lists the available interpolation settings." and "NSImageInterpolationHigh - Slower, higher-quality interpolation."
I'm fairly certain this applies to a named system image as well as any other.
Relevant subsection: Image Representations. "...with file-based images, most of the images you create need only a single image representation." and "You might create multiple representations in the following situations, however: For printing, you might want to create a PDF representation or high-resolution bitmap of your image."
You get the representation that suits the loaded image. You must create a PDF representation for a TIFF image, for example. To do so at high resolution, you'll need to refer back to the caching mode so you can get higher-res items.
There are a lot of fine details too numerous to list because of the high number of permutations of images/creation mechanisms/settings/ and what you want to do with it all. My post is meant to be a general guide toward finding the specific information you need for your situation.
For more detail, add specific details: the code you attempted to use, the type of image you're loading or creating -- you seemed to mention two different possibilities in your fourth paragraph -- and what went wrong.
我猜想图像以某种方式“硬连线”到图形系统中,并且它的 NSImage 表示只是一个数字,指示它是哪个硬连线图形。因此,您可能需要做的就是绘制它,然后捕获绘图。
非常一般地,创建一个将渲染图像的视图控制器,引用 VC 的
view
属性来渲染视图,提取 VC 的contentView
,获取 < code>contentView.layer,将layer
渲染到UIGraphics上下文中,从上下文中获取UIImage
,从UIImage中提取您想要的任何表示形式。(可能有一种更简单的方法,但这是我最终在一种情况下使用的方法。)
(而且,叹息,我想这个方案也不能保留缩放。)
I would guess that the image is "hard wired" into the graphics system somehow, and the NSImage representation of it is merely a number indicating which hard-wired graphic it is. So likely what you need to do is to draw it and then capture the drawing.
Very generally, create a view controller that will render the image, reference the VC's
view
property to cause the view to be rendered, extract thecontentView
of the VC, get thecontentView.layer
, render thelayer
into a UIGraphics context, get theUIImage
from the context, extract whatever representation you want from the UIImage.(There may be a simpler way, but this is the one I ended up using in one case.)
(And, sigh, I suppose this scheme doesn't preserve scaling either.)