Le 12 févr. 09 à 16:31, Smith, Steven (MCP) a écrit :

Hi folks,

I'm relatively new to Cocoa and need some direction on creating .png files.
I need to create 365 png files (one for each day of the year)
to be used as tags by other folks (eg "JAN01.png" "JAN2.png"..."DEC31.png").

I think I understand the draw/graphics concept, but I've been unable to get clear direction on how to transform(aka create) the picture to 48x48 .png files.

I'm not looking for code (its welcome of course), but a direction on the 'bridge'
between create/displaying an image and saving it to a file.

Thanks in advance,


All drawing code require a valid graphic context.
When you draw on screen (in an NSView for example), the framework setups a valid 'on screen' context before calling the drawRect: method.

If you want to draw into an image, you just have to setup a graphic context on an image, and call your drawing code.

A simple way to do this it to create an NSImage, and then using lockFocus, you can get a graphics context to draw in this image (just like you draw on screen).

Conceptually:

NSImage *img = [[NSImage alloc] initWithSize:48, 48];
[img lockFocus];

// you drawing code goes here

[img unlockFocus]

// Now, you can save your image using standard NSImage functions.

Unfortunately, I don't think you can save an NSImage as a PNG (it only supports the TIFFRepresentation method).


To have a greater control over the output format, you have to create a bitmap (NSBitmapImageRep)

And so, you will have a chance to use the longuest Cocoa method name:

- (id)initWithBitmapDataPlanes:(unsigned char **)planes pixelsWide: (NSInteger)width pixelsHigh:(NSInteger)height bitsPerSample: (NSInteger)bpssamplesPerPixel:(NSInteger)spp hasAlpha:(BOOL)alpha isPlanar:(BOOL)isPlanar colorSpaceName:(NSString *)colorSpaceName bitmapFormat:(NSBitmapFormat)bitmapFormatbytesPerRow: (NSInteger)rowBytes bitsPerPixel:(NSInteger)pixelBits

Create your image rep using this method.
create a graphics context using +[NSGraphicsContext graphicsContextWithBitmapImageRep:]

Save the current graphic state +[NSGraphicsContext saveGraphicsState]
Set your new context as the current context [NSGraphicsContext setCurrentContext:myContext];

Call your drawing function.

restore the graphic state +[NSGraphicContext restoreGraphicsState];

And now, your bitmap image is ready to be saved (using representationUsingType:).



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to