How to draw a NSImage like images in NSButtons (with a deepness)?

前端 未结 4 1692
遥遥无期
遥遥无期 2020-12-22 18:37

Is there any way to draw an NSImage like images in NSButtons or other cocoa interface elements?

Here are examples:

4条回答
  •  轮回少年
    2020-12-22 18:44

    To get to draw correctly within any rect, the CGContextDrawImage and CGContextFillRect for the inner mask must have the origin of (0,0). then when you draw the image for the inner shadow you can then reuse the mask rect. So ends up looking like:

    CGRect cgRect = CGRectMake( 0, 0, maskRect.size.width, maskRect.size.height );    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef maskContext = CGBitmapContextCreate( NULL, CGImageGetWidth( maskImage ), CGImageGetHeight( maskImage ), 8, CGImageGetWidth( maskImage ) * 4, colorSpace, kCGImageAlphaPremultipliedLast );
    CGColorSpaceRelease( colorSpace );
    CGContextSetBlendMode( maskContext , kCGBlendModeXOR );
    CGContextDrawImage( maskContext, cgRect, maskImage );
    CGContextSetRGBFillColor( maskContext, 1.0, 1.0, 1.0, 1.0 );
    CGContextFillRect( maskContext, cgRect );
    CGImageRef invertedMaskImage = CGBitmapContextCreateImage( maskContext );
    
    CGContextDrawImage( context, maskRect, invertedMaskImage );
    CGImageRelease( invertedMaskImage );
    CGContextRelease( maskContext );
    CGContextRestoreGState( context );
    

    You also have to leave a 1px border around the outside of the image or the shadows won't work correctly.

提交回复
热议问题