Re: Problem copying pdfImageRep to bitmapRep.
Re: Problem copying pdfImageRep to bitmapRep.
- Subject: Re: Problem copying pdfImageRep to bitmapRep.
- From: Heinrich Giesen <email@hidden>
- Date: Thu, 10 Jul 2008 22:35:27 +0200
On 10.07.2008, at 20:19, Graham Cox wrote:
I want to render pdf
data into a bitmap as part of an export conversion. My pdf data is
good (I can save it as a pdf file and that comes up OK). My bitmap is
good, but it's blank.
.... follows the code .....
If I understand the code and what you want to achieve it is clear that
the result is blank (could also be a random noise) because you created
an empty NSBitmapImageRep (bmRep ) and no one draws into the bmRep.
You say: "but I did a lockFocusOnRepresentation:bmRep" and expect that
drawing goes into the bmRep. This is a big but common misunderstanding.
A drawing (within a lockFocus --- unlockFocus) never changes/overwrites
an NSImageRep contained in the NSImage. With lockFocus you cannnot
draw/write into an NSBitmapImageRep. Instead always an
NSCachedImageRep is
created which is only good for drawing. A resulting NSBitmapImageRep
can be created from this NSCachedImageRep with the help of -
TIFFRepresentation.
In your code you don't need the NSBitmapImageRep (bmRep ), it is
absolutely useless.
The following code (modification of your code) could do it:
- (NSBitmapImageRep*) bitmapWithResolution:(int) dpi
{
NSPDFImageRep* pdfRep = [NSPDFImageRep imageRepWithData:[self
pdf]];
NSAssert( pdfRep != nil, @"couldn't create pdf image rep");
NSSize bmSize = [self drawingSize];
NSRect destRect = NSZeroRect;
destRect.size = bmSize;
NSImage* bmImage = [[NSImage alloc] initWithSize:bmSize];
[bmImage lockFocus];
[pdfRep drawInRect:destRect];
[bmImage unlockFocus];
[bmImage autorelease];
return [NSBitmapImageRep imageRepWithData:[bmImage
TIFFRepresentation]];
}
But there is a way to draw into an NSBitmapImageRep. You have to use
-graphicsContextWithBitmapImageRep: from NSGraphicsContext. This
has the advantage that you can set several parameters of the newly
created NSBitmapImageRep e.g. pixelsWide, pixelsHigh and size which
results in a resolution (dpi) of your choise. In your case I would
prefer this way to create a bitmap from a PDF file.
Examples of the use of -graphicsContextWithBitmapImageRep: are in
this list.
Heinrich
--
Heinrich Giesen
email@hidden
_______________________________________________
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