Re: Cocoa-dev Digest, Vol 3, Issue 1203

  • 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
    <giesenH...>
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today