Skip navigation.
 
mlNSCustomImageRep won't draw
FROM : Lindsey Spratt
DATE : Wed Apr 13 00:30:36 2005

I'm trying to use NSImageCustomRep to  "capture" the drawing of an
arbitrary graphic and then draw the resulting NSImage so that I can use
compositing when displaying the graphic. This works well in the
CompositeLab example project, but the "captured" image doesn't draw
anything when I try it with my custom NSView, NSWindowController, and
NSDocument (XGPGraphicView, XGPGraphicsWindowController, and
XGPDrawDocument, respectively).

The initWithFrame:, drawCustom:, and drawRect: methods in
XGPGraphicView are show below. The initWithFrame: method sets up the
_source instance variable to hold the custom image. This image is
defined using the drawCustom: call-back. The drawRect: method uses
compositeToPoint:operation: method to cause the _source image to be
rendered (in theory).

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
       [NSColor setIgnoresAlpha:NO];
        NSMutableArray *dragTypes = [NSMutableArray
arrayWithObjects:NSColorPboardType, NSFilenamesPboardType, nil];
        [dragTypes addObjectsFromArray:[NSImage imagePasteboardTypes]];
        [self registerForDraggedTypes:dragTypes];
     _controller = nil;
        _selectedGraphics = [[NSMutableArray allocWithZone:[self zone]]
init];
        _creatingGraphic = nil;
        _rubberbandRect = NSZeroRect;
        _rubberbandGraphics = nil;
        _gvFlags.rubberbandIsDeselecting = NO;
        _gvFlags.initedRulers = NO;
        _editingGraphic = nil;
        _editorView = nil;
        _pasteboardChangeCount = -1;
        _pasteCascadeNumber = 0;
        _pasteCascadeDelta = NSMakePoint(XGPDefaultPasteCascadeDelta,
XGPDefaultPasteCascadeDelta);
        _gvFlags.snapsToGrid = NO;
        _gvFlags.showsGrid = NO;
        _gvFlags.knobsHidden = NO;
        _gridSpacing = 8.0;
        _gridColor = [[NSColor lightGrayColor] retain];
        _unhideKnobsTimer = nil;
       
       {
           NSRect sRect = NSMakeRect(0.0, 0.0, 100.0, 100.0);
           _source = nil;
           
            [(_source = [[NSImage allocWithZone:[self zone]]
initWithSize:sRect.size]) addRepresentation:[[[NSCustomImageRep alloc]
initWithDrawSelector:@selector(drawCustom:) delegate:self]
autorelease]];
            [_source setBackgroundColor:[NSColor clearColor]];
        }
    }
    return self;
}

- (void)drawCustom:(NSCustomImageRep *)imageRep
{
    NSRect sRect = NSMakeRect(0.0, 0.0, 100.0, 100.0);
   NSBezierPath *path = [NSBezierPath bezierPath];
   
    [path moveToPoint:NSMakePoint(0.0, 0.0)];
   [path lineToPoint:NSMakePoint(0.0, sRect.size.height)];
   [path lineToPoint:NSMakePoint(sRect.size.width, sRect.size.height)];
    [path closePath];

    [[NSColor redColor] set];
    [path fill];
}

- (void)drawRect:(NSRect)rect
{
    NSRect sRect = NSMakeRect(0.0, 0.0, 100.0, 100.0);

   // Erase the whole view
    [[NSColor whiteColor] set]; // set white as background color.
    NSRectFill([self bounds]);

    // Color for the frame of the image.
    [[NSColor blackColor] set];

    // Draw the source bitmap and then frame it with black
    [_source compositeToPoint:sRect.origin
operation:NSCompositeSourceOver];
    NSFrameRect(sRect);
}

Related mailsAuthorDate
mlNSCustomImageRep won't draw Lindsey Spratt Apr 13, 00:30
mlRe: NSCustomImageRep won't draw Lindsey Spratt Apr 13, 18:45