Re: newbie question on creating .png files

2009-02-13 Thread Steven Spencer
To get a NSBitmapImageRep from a NSImage, have a look at the Reducer sample 
code at location :

http://developer.apple.com/samplecode/Reducer/listing16.html

file : ImageReducer.m
routine : BitmapImageRepFromNSImage

at the end of the file.

- Steve





On 12.02.2009, at 17:14, Jean-Daniel Dupas wrote:


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


You can create a new NSBitmapImageRep using the TIFFRepresentation and  
use representationUsingType:properties:
to get the PNG-data:

 NSData* TIFFData = [img TIFFRepresentation];
 NSBitmapImageRep* bitmapImageRep = [NSBitmapImageRep  
imageRepWithData:TIFFData];
 NSData* PNGData = [bitmapImageRep  
representationUsingType:NSPNGFileType properties:nil];
 [PNGData writeToFile: @JAN01.png atomically: YES];

HTH,

felix


-- 
___

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


newbie question on creating .png files

2009-02-12 Thread Smith, Steven (MCP)
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,
Steven  ___

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


Re: newbie question on creating .png files

2009-02-12 Thread Jean-Daniel Dupas


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


Re: newbie question on creating .png files

2009-02-12 Thread Felix Franz


On 12.02.2009, at 17:14, Jean-Daniel Dupas wrote:



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




You can create a new NSBitmapImageRep using the TIFFRepresentation and  
use representationUsingType:properties:

to get the PNG-data:

NSData* TIFFData = [img TIFFRepresentation];
	NSBitmapImageRep* bitmapImageRep = [NSBitmapImageRep  
imageRepWithData:TIFFData];
	NSData* PNGData = [bitmapImageRep  
representationUsingType:NSPNGFileType properties:nil];

[PNGData writeToFile: @JAN01.png atomically: YES];

HTH,

felix
___

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


Re: newbie question on creating .png files

2009-02-12 Thread David Duncan

On Feb 12, 2009, at 8:28 AM, Felix Franz wrote:

You can create a new NSBitmapImageRep using the TIFFRepresentation  
and use representationUsingType:properties:

to get the PNG-data:

NSData* TIFFData = [img TIFFRepresentation];
	NSBitmapImageRep* bitmapImageRep = [NSBitmapImageRep  
imageRepWithData:TIFFData];
	NSData* PNGData = [bitmapImageRep  
representationUsingType:NSPNGFileType properties:nil];

[PNGData writeToFile: @JAN01.png atomically: YES];



While this might be fine for a one-off application where performance  
and memory usage doesn't matter, this is really not the best way to  
convert an NSImage into a PNG. In fact, imageRepWithData: should work  
with any file type that is supported by ImageIO, which natively  
includes PNG, so you could pass in the original file data rather than  
going through NSImage.

--
David Duncan
Apple DTS Animation and Printing

___

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