Re: Cocoa-dev Digest, Vol 3, Issue 1203
Re: Cocoa-dev Digest, Vol 3, Issue 1203
- Subject: Re: Cocoa-dev Digest, Vol 3, Issue 1203
- From: Heinrich Giesen <email@hidden>
- Date: Mon, 2 Oct 2006 17:04:02 +0200
Fabio Mancinelli wrote:
I would like to do a sort-of slideshow using pages from a PDF file.
(images are pixel based)
No, images are not pixel based, they do not even know what a pixel is.
NSImages are only containers of zero, one or more "real images",
which means objects of subclasses of NSImageRep. e.g. NSEpsImageRep,
NSPDFImageRep
which may have or may not have pixels.
In my applications I show PDF files in a much simpler way (which may be
way too simple for what you want to dore doing)
Precondition a: the file to load contains PDF-Data
Precondition b: you have subclassed NSView (MyPDFView) and
have an instance of it (myPDFView) and implement the drawRect:
method
1) create a NSPDFImageRep
NSPDFImageRep *pdfRep = [[NSImageRep
imageRepWithContentsOfFile:filename] retain];
// be cautious
if( ![pdfRep isKindOfClass:[NSPDFImageRep class]] ) ERRORCODE;
2) do not forget to release pdfRep in:
-(void) dealloc
{
[pdfRep release];
...
[super dealloc];
}
3) draw a page of the PDF-imageRep
// should be implemented in MyPDFView.m
-(void) drawRect:(NSRect)rect
{
// do something to prepare drawing (if needed)
[pdfRep setCurrentPage:curPage];
// curPage is an int value between 0 and [pdfRep pageCount]-1
// and is globally managed
[pdfRep drawInRect:[myPDFView frame];
// from the docs for drawInRect:
// Draws the image, scaling it (as needed) to fit the specified
rectangle.
// should read: Draws the "imageRep" ....
}
Remark: Drawing is not optimal because not only "rect" is drawn but
the complete view.
If you want to do a more optimized drawing, here is what I would do:
NSPDFImageRep *pdfRep = [NSImageRep
imageRepWithContentsOfFile:filename];
NSImage *imageForDrawing = [[NSImage alloc]
initWithSize:NSZeroSize];
[imageForDrawing addRepresentation: pdfRep];
[imageForDrawing setScalesWhenResized:YES];
and in MyPDFView.m
-(void) drawRect:(NSRect)rect
{
// do something to prepare drawing (if needed)
[pdfRep setCurrentPage:curPage];
if( <curPage changed> ){
[imageForDrawing recache];
}
[imageForDrawing drawInRect:rect fromRect:rect
operation:NSCompositeSourceOver fraction:1.0];
}
and in -(void)dealloc { [imageForDrawing release]; ... }
--
Heinrich Giesen
email@hidden
_______________________________________________
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