On 31 Dec 2013, at 6:15 AM, Leonardo <mac.iphone....@gmail.com> wrote:

> In my NSView subclass, I have to draw an image within a bezierPath and apply
> an NSShadow to the result.
> So within the drawRect: method I add
> 
>     [bezierPath addClip];
>     [mImage drawInRect:inRect fromRect:fromRect
> operation:NSCompositeSourceOver fraction:mOpacity];
> 
> It works. The image gets clipped by the path.
> Now I would like to apply the NSShadow to the result, so before those lines
> above I add
> 
> [bezierPath addClip];
> [mShadow set];
> [mImage drawInRect:inRect fromRect:fromRect operation:NSCompositeSourceOver
> fraction:mOpacity];
> 
> I can't get the shadow drawn because the bezierPath is clipping the drawing
> area where the shadow should fall.
> How to solve this trouble?
> I have tried to use a layer, but I get weird results when I resize, rotate
> and modify other parameters of the view.

This worked for me:

====
- (void) drawRect:(NSRect)dirtyRect
{
    NSBezierPath *      bezierPath = [[NSBezierPath alloc] init];
    [bezierPath appendBezierPathWithRoundedRect: self.clipPathRect
                                        xRadius: 10.0 yRadius: 10.0];

    [bezierPath setLineWidth: 0.0];

    NSGraphicsContext *     context = [NSGraphicsContext
                                       graphicsContextWithWindow: self.window];

    //  Clear the view
    [context saveGraphicsState]; {
        [[NSColor whiteColor] set];
        [NSBezierPath fillRect: dirtyRect];
    } [context restoreGraphicsState];

    //  Stroke the (zero-width) clipping path
    [context saveGraphicsState]; {
        [self.shadow set];
        [bezierPath stroke];
        //  No width means no visible boundary line,
        //  but the shadow is still drawn.
    } [context restoreGraphicsState];

    //  Draw the image, clipped to the path
    [context saveGraphicsState]; {
        [bezierPath addClip];
        [_image drawInRect: self.clipPathRect
                  fromRect: self.selectedSourceRect
                 operation: NSCompositeSourceOver
                  fraction: self.imageFraction];
        //  The image obscures the inner shadow.
        //  Bug: The shadow shows through to the extent imageFraction < 1.
        //  Can the shadow drawing be clipped to the inverse of the clipping 
path?
    } [context restoreGraphicsState];
}
====

        — F


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to