• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: newbie question on creating .png files
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: newbie question on creating .png files


  • Subject: Re: newbie question on creating .png files
  • From: Jean-Daniel Dupas <email@hidden>
  • Date: Thu, 12 Feb 2009 17:14:10 +0100


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 (email@hidden)

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: newbie question on creating .png files
      • From: Felix Franz <email@hidden>
References: 
 >newbie question on creating .png files (From: "Smith, Steven (MCP)" <email@hidden>)

  • Prev by Date: Re: How do I make one thread wait until another thread exits?
  • Next by Date: Re: Garbage Collection and NSManagedObject
  • Previous by thread: newbie question on creating .png files
  • Next by thread: Re: newbie question on creating .png files
  • Index(es):
    • Date
    • Thread