• 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: Rasterizing an NSString to a bitmap
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Rasterizing an NSString to a bitmap


  • Subject: Re: Rasterizing an NSString to a bitmap
  • From: p3consulting <email@hidden>
  • Date: Tue, 8 Jun 2004 12:01:25 +0200

> 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

Example:

// You need this C function
void setCGContextInCurrentNSGraphicsContext (CGContextRef newContext)
{
((graphicsContextInstance) [NSGraphicsContext
currentContext])->context = newContext;
}


// 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
);


NSImage *dummy = [[NSImage alloc] initWithSize:imageSize];
[dummy lockFocus] ;

CGContextRef savedCGContext = [[NSGraphicsContext currentContext]
graphicsPort];
setCGContextInCurrentNSGraphicsContext (bitmapCGContext);


// do your drawing here
// using CG... functions or NS... methods

setCGContextInCurrentNSGraphicsContext (savedCGContext);

[dummy unlockFocus] ;
[dummy release] ;


NSImage *resultImage = [[NSImage alloc] initWithSize:imageSize] ;
[resultImage addRepresentation:bitmapRep] ;


// resultImage now contains what you have drawn in the bitmapRep
// you can use it as usual [ibImageView setImage:resultImage] or
whatever...


Pascal Pochet
email@hidden
----------------------------------
PGP
KeyID: 0x208C5DBF
Fingerprint: 9BFB 245C 5BFE 7F1D 64B7 C473 ABB3 4E83 208C 5DBF

[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
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.


  • Follow-Ups:
    • Re: Rasterizing an NSString to a bitmap
      • From: Marcel Weiher <email@hidden>
References: 
 >Rasterizing an NSString to a bitmap (From: John Stiles <email@hidden>)
 >Re: Rasterizing an NSString to a bitmap (From: Marcel Weiher <email@hidden>)

  • Prev by Date: Re: Retain cycle problem with bindings & NSWindowController
  • Next by Date: Binding a pop-up menu to a dictionary in a tableColumn
  • Previous by thread: Re: Rasterizing an NSString to a bitmap
  • Next by thread: Re: Rasterizing an NSString to a bitmap
  • Index(es):
    • Date
    • Thread