Re: Rasterizing an NSString to a bitmap
Re: Rasterizing an NSString to a bitmap
- Subject: Re: Rasterizing an NSString to a bitmap
- From: John Stiles <email@hidden>
- Date: Tue, 8 Jun 2004 10:26:51 -0700
Yes, this is a cute hack, but it's not documented and will almost
certainly break one day.
I'd rather do it in a more supported way.
On Jun 8, 2004, at 4:26 AM, Marcel Weiher wrote:
On 8 Jun 2004, at 11:01, p3consulting wrote:
That's the problem right there. You can't make an NSImage draw into
a NSBitmapImageRep you provide. You can only get an
NSBitmapImageRep out of an NSImage, using the almost but not
entirely appropriately named -initWithFocusedViewRect: method.
Wrong: that's the purpose of NSImage's method:
- (void)addRepresentation:(NSImageRep *)imageRep
Actually, that adds new image-reps for reading. So you can add a PDF
rep, a Postscript rep, and TIFF/other image reps at different
resolutions. You *can* also use it to add cached-image reps, but how
that interacts isn't actually documented.
Example:
// You need this C function
void setCGContextInCurrentNSGraphicsContext (CGContextRef newContext)
{
((graphicsContextInstance) [NSGraphicsContext
currentContext])->context = newContext;
}
DANGER!
That is not a supported operation, and when I specifically asked at a
WWDC a while ago for a method for creating a NSGraphicsContext from a
CoreGraphics context, I was told that this wasn't possible because of
special, private setup that requires work.
Just jamming the CG context into an existing NSGraphicsContext is an
interesting technique, but if it works, then that is probably just
lucky coincidence.
It *is* an interesting hack, though ;-)
// And here the code creating a NSImage from a drawing done in a
NSBitmapImageRep
// use you own context dependant settings (bitsPerSample, alpha or
not, color space name,...
// and assuming you have NSSize imageSize declared somewhere...
NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:nil pixelsWide:(int)imageSize.width
pixelsHigh:(int)imageSize.height bitsPerSample:8 samplesPerPixel:4
hasAlpha:YES
isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bytesPerRow:0
bitsPerPixel:0];
CGContextRef bitmapCGContext = CGBitmapContextCreate ( [bitmapRep
bitmapData],
(int)imageSize.width, (int)imageSize.height,
8,
[bitmapRep bytesPerRow],
CGColorSpaceCreateDeviceRGB(),
kCGImageAlphaPremultipliedLast
);
Apart from the issues above, CGBitmapContextCreate() actually only
supports a limited number of parameters. So it is not true that you
can just draw onto an arbitrary NSBitmapImageRep.
[rest of hack deleted]
Still: interesting!
Cheers,
Marcel
--
Marcel Weiher Metaobject Software Technologies
email@hidden www.metaobject.com
Metaprogramming for the Graphic Arts. HOM, IDEAs, MetaAd etc.
1d480c25f397c4786386135f8e8938e4
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.