IconRef and NSImageRep

  • I'm trying to make an NSImageRep subclass that draws IconRefs, but it
    isn't working - nothing appears on the screen.

    I get the IconRef by calling
    GetIconRef
    (kOnSystemDisk,kSystemIconsCreator,kGenericFolderIcon,&iconRef), which
    succeeds by all appearances.

    Then I have this method in my NSImageRep subclass:

    -(void)draw
    {
      NSSize mySize = [self size];
      CGRect drawRect = CGRectMake(0,0,mySize.width,mySize.height);

      PlotIconRefInContext((CGContextRef)[[NSGraphicsContext
    currentContext] graphicsPort],

    &drawRect
    ,kAlignNone,kTransformNone,NULL,kPlotIconRefNormalFlags,iconRef);
    }

    In the debugger, mySize and drawRect have the expected values
    (0,0,48,48), but nothing draws. This is in a table cell, and other
    cells that use images loaded from files appear correctly.

    Originally I used [NSWorkspace iconForFileType:], but the resulting
    image didn't draw well at 48x48. It appeared to be scaled down from
    the 128x128 image with no smoothing applied. I tried it with and
    without using setScalesWhenResized.

    So, what could I be doing wrong in my NSImageRep? Should I go back to
    the NSWorkspace approach and try something different?

    Thanks,

    --
    David Catmull
    <uncommon...>
    http://www.uncommonplace.com/
  • The following code may help you:

    {
    NSImage *  image  = nil;
    NSImage *  outImage = nil;
    NSData  *  imageRep = nil;
    NSSize  imageSize = NSMakeSize(48.0,48.0);

    outImage = [[NSImage alloc] initWithSize:imageSize];
    image = [[[NSWorkspace sharedWorkspace] iconForFile:@"/Applications/
    Safari.app"] retain];

    [image setScalesWhenResized:YES];
    [outImage lockFocus];
    [image setSize:imageSize];
    [[NSGraphicsContext currentContext]
    setImageInterpolation:NSImageInterpolationHigh];
    [image compositeToPoint:NSZeroPoint operation: NSCompositeSourceOver];
    [outImage unlockFocus];
    [image release];

    imageRep = [outImage TIFFRepresentation];

    [outImage release];
    }

    Liu Qi

    On Dec 20, 2007, at 3:14 AM, David Catmull wrote:

    > I'm trying to make an NSImageRep subclass that draws IconRefs, but
    > it isn't working - nothing appears on the screen.
    >
    > I get the IconRef by calling GetIconRef
    > (kOnSystemDisk,kSystemIconsCreator,kGenericFolderIcon,&iconRef),
    > which succeeds by all appearances.
    >
    > Then I have this method in my NSImageRep subclass:
    >
    > -(void)draw
    > {
    > NSSize mySize = [self size];
    > CGRect drawRect = CGRectMake(0,0,mySize.width,mySize.height);
    >
    > PlotIconRefInContext((CGContextRef)[[NSGraphicsContext
    > currentContext] graphicsPort],
    >
    > &drawRect,kAlignNone,kTransformNone,NULL,kPlotIconRefNormalFlags,iconR
    > ef);
    > }
    >
    > In the debugger, mySize and drawRect have the expected values
    > (0,0,48,48), but nothing draws. This is in a table cell, and other
    > cells that use images loaded from files appear correctly.
    >
    > Originally I used [NSWorkspace iconForFileType:], but the resulting
    > image didn't draw well at 48x48. It appeared to be scaled down from
    > the 128x128 image with no smoothing applied. I tried it with and
    > without using setScalesWhenResized.
    >
    > So, what could I be doing wrong in my NSImageRep? Should I go back
    > to the NSWorkspace approach and try something different?
    >
    > Thanks,
    >
    > --
    > David Catmull
    > <uncommon...>
    > http://www.uncommonplace.com/
    >
previous month december 2007 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