从视图、字符串等创建图像
好吧,我有一个问题。我有一种名为 imageFromText 的方法,它需要一个参数,即字符串本身,并且返回一个 NSImage。我还有另一个叫做:imageFromView,这个基本上必须“截取视图”并返回一个 NSImage,它也只有一个参数,即视图本身。所以它看起来像这样:
-(NSImage*)imageFromText: (NSString*)text {
}
-(NSImage*)imageFromView: (NSView*)view {
}
只有一个问题,我不知道该怎么做。好吧,我花了一个下午的时间四处寻找,但什么也没找到。对于第二个方法,我尝试过 dataWithPDFInsideRect 方法,但显然,该方法不是针对此建议而提出的。请帮帮我! 请不要:我不是要求准备好代码。就像古老的口述(在我的县):不要给他们鱼,而是教他们如何钓鱼。 (翻译)。
Ok so I have a problem. I have a method that is called imageFromText, it requires one parameter, the string itself, and it returns a NSImage. I also have another one which is called: imageFromView, this one basically have to "take a screenshot" of the view and return a NSImage, it also has only one parameter, the view itself. So it looks like this:
-(NSImage*)imageFromText: (NSString*)text {
}
-(NSImage*)imageFromView: (NSView*)view {
}
There's only one problem, I have no idea how to do this. Well so, I spent my afternoon searching around and I didn't find nothing. I've tried, to the second one, a method dataWithPDFInsideRect, but obviously, this method was not made to this propose. Please help me out!.
PLEASE NOT: I'M NOT ASKING FOR THE CODE READY. LIKE THE OLD DICTATION (IN MY COUNTY): DON'T GIVE THEM THE FISH, THEACH THEM HOW TO FISH. (TRANSLATED).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
总结一下,将焦点锁定在图像上,然后进行绘制。如果您搜索 lockFocus,NSImage 文档应该有更多内容。
Summarizing, lock focus on the image, then draw. The NSImage docs should have more if you search for lockFocus.
您感兴趣的方法是:
要绘制图像,分配一个图像,然后将焦点锁定在该图像上,然后发出绘图调用,然后解锁焦点。
要将视图绘制到图像中,请将焦点锁定在图像上,获取当前图形上下文(现在是图像),并将其传递给 -[NSView displayRectIgnoringOpacity:inContext:]。
The methods you're interested in are:
To draw to an image, allocate one, and then lock focus on it, then issue drawing calls and then unlock focus.
To draw a view into an image, lock focus on an image, get the current graphic context (which is now the image), and pass that to -[NSView displayRectIgnoringOpacity:inContext:].
另一种方法是将焦点锁定在视图上,然后创建位图图像代表 的内容 视图的边界。然后您可以 创建一个空白图像,其大小为边界大小,并且向其中添加图像代表。
第三种方法是
dataWithPDFInsideRect:
。是的,就是您尝试过但无法正常工作的那个(我希望您能解释一下您遇到的问题,而不是直接忽略它!)。传递视图的边界,然后将数据传递给 NSImage 的 initWithData: 。An alternative way is to lock focus on the view, then create a bitmap image rep with the contents of the view's bounds. You can then create a blank image whose size is the size of the bounds, and add the image rep to it.
The third way is
dataWithPDFInsideRect:
. Yes, the one you tried and couldn't get to work (I wish you'd explained what problem you had with it instead of just dismissing it!). Pass the view's bounds, then pass the data to NSImage'sinitWithData:
.至于 imageFromView:请查看 Organizer 文档中的
屏幕捕获
。imageFromText:你想要一个只显示文本的图像(我假设是PNG)?您不想指定图像大小、字体大小、字体颜色、背景颜色等内容吗?
As for imageFromView: check the Organizer Documentation for
Screen Capture
.And imageFromText: You want an image (PNG I assume) that just shows a text? Don't you want to specify things like image size, font size, font color, background color, ...?