• 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: Programatically creating/saving an NSImage;
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Programatically creating/saving an NSImage;


  • Subject: Re: Programatically creating/saving an NSImage;
  • From: Ricky Sharp <email@hidden>
  • Date: Mon, 21 Mar 2005 16:04:25 -0600


On Mar 21, 2005, at 3:23 PM, Matt Budd (Madentec) wrote:

I am trying to draw into an NSImage and then save that to a JPG or a TIFF. Here is my code:

float lfWidth = 25;
float lfHeight = 50;
NSImage *loTestImage = [[NSImage alloc] initWithSize: NSMakeSize(lfWidth, lfHeight)];
NSBitmapImageRep *loTestImageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
pixelsWide: lfWidth
pixelsHigh: lfHeight
bitsPerSample: 8
samplesPerPixel: 4
hasAlpha: YES
isPlanar: NO
colorSpaceName: NSDeviceRGBColorSpace
bytesPerRow: 0
bitsPerPixel: 0
];
[loTestImage addRepresentation: loTestImageRep];
[loTestImage lockFocus];


//Here I would do more complicated NSBezierPath stuff
[[NSColor redColor] set];
NSRectFill(NSMakeRect(0, 0, lfWidth, lfHeight));

[loTestImage unlockFocus];


[[loTestImageRep TIFFRepresentation] writeToFile: @"/test.tif" atomically: YES];
[[loTestImageRep representationUsingType: NSJPEGFileType properties: nil] writeToFile: @"/test.jpg" atomically: YES];


[loTestImageRep release];
[loTestImage release];


However, when I run this code, I get two images of the correct size (one TIFF and one JPG), but they don't contain the solid red color, they are random looking pictures. It looks as if I am setting the actual pixels to be some random memory somewhere? Can anyone point me to what I am doing wrong?

I'm not sure if the rep that is being locked is the one that you created.


So, rather than calling lockFocus on the NSImage (which will in turn lock what it thinks is the best representation), use lockFocusOnRepresentation to specifically lock the image rep you created.

___________________________________________________________
Ricky A. Sharp         mailto:email@hidden
Instant Interactive(tm)   http://www.instantinteractive.com

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Programatically creating/saving an NSImage;
      • From: Matt Budd (Madentec) <email@hidden>
References: 
 >Programatically creating/saving an NSImage; (From: Matt Budd (Madentec) <email@hidden>)

  • Prev by Date: Programatically creating/saving an NSImage;
  • Next by Date: Re: Programatically creating/saving an NSImage;
  • Previous by thread: Programatically creating/saving an NSImage;
  • Next by thread: Re: Programatically creating/saving an NSImage;
  • Index(es):
    • Date
    • Thread