Help with NSImage and NSString...

Nibbie
Posts: 1
Joined: 2010.11
Post: #1
Hi,
I've been trying to simply take an NSImage, say "/Pictures/image.jpg" and add NSString, @"Example Text" for example, to it at a specified NSColor and Times as the font at an X and Y coordinate. I have completely and utterly failed Rasp. I have managed to load the image, and I have managed to draw text, but when I combine them in a loop it quits the second time. Does anyone have any code that will load an image and draw text to it? If not can anyone experianced spare 3 minutes and try your hand at it?
Thanks,
Joseph [iHateNSEverything] Duchesne
Quote this message in a reply
Member
Posts: 257
Joined: 2004.06
Post: #2
Joseph Duchesne Wrote:Hi,
I've been trying to simply take an NSImage, say "/Pictures/image.jpg" and add NSString, @"Example Text" for example, to it at a specified NSColor and Times as the font at an X and Y coordinate. I have completely and utterly failed Rasp. I have managed to load the image, and I have managed to draw text, but when I combine them in a loop it quits the second time. Does anyone have any code that will load an image and draw text to it? If not can anyone experianced spare 3 minutes and try your hand at it?
Thanks,
Joseph [iHateNSEverything] Duchesne

Do you mean, draw text directly onto the image data itself or draw text and an image in say, the drawRect method of a sub-classed NSView? I think somewhere on my HD I have examples of doing both which is unfortunately at home so I won't be able to post it until later tonight.

If I recall, though, what I ended up doing for drawing text directly onto an image was creating a new NSImage object (using the alloc then initWithSize methods) and calling lockFocus on it. After that, any draw commands are sent to the image so calling [someNSString drawAtPoint: somePoint withAttributes: someDict] will draw the text onto the image. Oh, and [someNSImage compositeToPoint: somePoint operation: op] will draw the image there too. Though you'll probably want to draw the image first then the text. And then call unlockFocus when you're done.

I've used this technique both for creating bitmaps with text on them and for combining images together.

Edit: Okay, I found some code on my HD at work that kinda illustrates what I'm talking about here.

Code:
NSImage* myImage = [NSImage imageNamed: @"/Pictures/image.jpg"];

NSImage* newImage = [[NSImage alloc] initWithSize: [myImage size]];
[newImage lockFocus];

[myImage compositeToPoint: NSMakePoint(0, 0) operation: NSCompositeCopy];

/* if you have a second image you're going to overlay on top of the first, do the same except use NSCompositeSourceOver as the operation */

NSString* myString = @"Example Text";
[myString drawAtPoint: NSMakePoint(0, 0) withAttributes: textAttributes];

[newImage unlockFocus];

The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #3
http://www.geocities.com/ed72678954/GlyphTool.zip
This application (source code included) draws NSStrings to NSImages.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Basic NSImage drawing help please! xenocide 6 5,605 Feb 2, 2009 11:01 AM
Last Post: FlamingHairball
  NSImage and PNG's bmantzey 3 5,023 Nov 29, 2008 07:01 PM
Last Post: arekkusu
  NSImage PDF -> JPG unknown 3 3,887 Oct 23, 2005 08:36 PM
Last Post: PowerMacX
  How to get resolution of an NSImage or NSBitmapImageRep? aegidian 4 5,637 Oct 18, 2005 02:39 AM
Last Post: aegidian
  NSImage -> Jigsaw peices unknown 5 3,678 Sep 29, 2005 07:00 AM
Last Post: unknown