Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 如何结合CGPathCreateCopyByDashingPath()和CGPathCreateCopyByStrokingPath()在OS X上绘制虚线CGPath?_Macos_Cocoa_Core Graphics_Quartz Graphics - Fatal编程技术网

Macos 如何结合CGPathCreateCopyByDashingPath()和CGPathCreateCopyByStrokingPath()在OS X上绘制虚线CGPath?

Macos 如何结合CGPathCreateCopyByDashingPath()和CGPathCreateCopyByStrokingPath()在OS X上绘制虚线CGPath?,macos,cocoa,core-graphics,quartz-graphics,Macos,Cocoa,Core Graphics,Quartz Graphics,Core Graphics有两个函数,CGPathCreateCopyByDashingPath()和CGPathCreateCopyByStrokingPath(),这两个函数都采用要笔划的CGPath并将其转换为等效填充。我需要这样做,例如,我可以用渐变笔划一条线:调用CGPathCreateCopyByStrokingPath(),将路径加载到CGContext中,调用CGContextClip(),然后绘制渐变 但是,CGPathCreateCopyByStrokingPath()接受线

Core Graphics有两个函数,
CGPathCreateCopyByDashingPath()
CGPathCreateCopyByStrokingPath()
,这两个函数都采用要笔划的CGPath并将其转换为等效填充。我需要这样做,例如,我可以用渐变笔划一条线:调用
CGPathCreateCopyByStrokingPath()
,将路径加载到CGContext中,调用
CGContextClip()
,然后绘制渐变

但是,
CGPathCreateCopyByStrokingPath()
接受线条笔划参数,如线条帽、线条连接等,而
CGPathCreateCopyByDashingPath()
不接受。我想能够冲刺与定制线帽/连接

特别是,两个职能部门的文件中都有以下内容:

创建新路径时,填充新路径绘制的像素与使用指定的划线参数笔划原始路径相同

创建新路径时,填充新路径绘制的像素与笔划原始路径相同

我的。所以我从中得到的是,一旦你调用了其中一个函数,你就会得到一条新的路径,它由限制所请求笔划的线组成。因此,如果我用短划线调用
,然后用行程
调用
,第一个将创建一个由一堆小矩形组成的路径,然后第二个将生成形成这些小矩形周长线的矩形,这不是我想要的。(我可以对此进行测试,稍后再发布图片。)

我所看到的一切都指向核心图形,它能够直接在CGContext中实现这一点;例如,“使用Quartz编程”一书在其短线示例中显示了圆形和方形的线帽。有什么理由我不能用一个独立的CGPath做这件事吗

我错过什么了吗?还是我只是被这件事缠住了

这是针对OS X的,不是针对iOS的。


谢谢。

我真的不确定问题出在哪里。你有一条路要走;让我们称之为
A
。调用
CGPathCreateCopyByDashingPath()
a
创建一个新路径,该路径具有所需的短划线;让我们称之为
B
<代码>B
没有在其上设置特定的线帽/连接,因为这不是路径的属性,而是在笔划路径时使用的属性。(想象一下,通过从一点到另一点绘制线段,手工绘制一条虚线路径;在该路径描述中,没有任何大写或连接的概念,每个线段只有起点和终点。)然后取
B
,调用
CGPathCreateCopyByStrokingPath()
,得到
C
,使用特定线宽/封口/连接特征的
B
笔划的可填充路径。最后,使用渐变填充填充
C
。那不行吗?看起来你知道解决问题所需的所有组件,所以我不确定问题到底在哪里;你能澄清一下吗?

原来
CGPathCreateCopyByDashingPath()
的文档是错误的

现在,它说

创建新路径时,填充新路径绘制的像素与使用指定的划线参数笔划原始路径相同

这意味着它使用默认笔划参数生成结果路径。但事实并非如此!相反,您会得到一个新路径,该路径只是被虚线参数分割的现有路径。您需要调用
CGPathCreateCopyByStrokingPath()
来生成要填充的路径

以下程序分为三个部分。首先,它通过使用
CGContext
函数而不是
CGPath
函数绘制来显示路径的外观。其次,它只使用
CGPathCreateCopyByDashingPath()
进行绘制。请注意,笔划路径不会在破折号原来所在的位置生成一堆方框,而是生成一堆破折号。如果你仔细看,你会看到一个非常小的蓝色的病号在线路连接处。最后,它调用
CGPathCreateCopyByDashingPath()
,然后调用
CGPathCreateCopyByStrokingPath()
,您将看到填充生成正确的输出

再次感谢你,巴勒!我不确定文档应该更改为什么,或者如何请求这样的更改

// 15 october 2015
#import <Cocoa/Cocoa.h>

@interface dashStrokeView : NSView
@end

void putstr(CGContextRef c, const char *str, double x, double y)
{
    NSFont *sysfont;
    CFStringRef string;
    CTFontRef font;
    CFStringRef keys[1];
    CFTypeRef values[1];
    CFDictionaryRef attrs;
    CFAttributedStringRef attrstr;
    CTLineRef line;

    sysfont = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSRegularControlSize]];
    font = (CTFontRef) sysfont;     // toll-free bridge

    string = CFStringCreateWithCString(kCFAllocatorDefault,
        str, kCFStringEncodingUTF8);
    keys[0] = kCTFontAttributeName;
    values[0] = font;
    attrs = CFDictionaryCreate(kCFAllocatorDefault,
        keys, values,
        1,
        &kCFTypeDictionaryKeyCallBacks,
        &kCFTypeDictionaryValueCallBacks);
    attrstr = CFAttributedStringCreate(kCFAllocatorDefault, string, attrs);

    line = CTLineCreateWithAttributedString(attrstr);
    CGContextSetTextPosition(c, x, y);
    CTLineDraw(line, c);

    CFRelease(line);
    CFRelease(attrstr);
    CFRelease(attrs);
    CFRelease(string);
}

@implementation dashStrokeView

- (void)drawRect:(NSRect)r
{
    CGContextRef c;
    CGFloat lengths[2] = { 10, 13 };
    CGMutablePathRef buildpath;
    CGPathRef copy, copy2;

    c = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];

    CGContextSaveGState(c);

    putstr(c, "Dash + Stroke With CGContext Functions", 10, 10);
    CGContextMoveToPoint(c, 50, 50);
    CGContextAddLineToPoint(c, 100, 30);
    CGContextAddLineToPoint(c, 150, 70);
    CGContextAddLineToPoint(c, 200, 50);
    CGContextSetLineWidth(c, 10);
    CGContextSetLineJoin(c, kCGLineJoinBevel);
    CGContextSetLineCap(c, kCGLineCapRound);
    CGContextSetLineDash(c, 0, lengths, 2);
    CGContextSetRGBStrokeColor(c, 0, 0, 0, 1);
    CGContextStrokePath(c);
    // and reset
    CGContextSetLineWidth(c, 1);
    CGContextSetLineJoin(c, kCGLineJoinMiter);
    CGContextSetLineCap(c, kCGLineCapButt);
    CGContextSetLineDash(c, 0, NULL, 0);

    CGContextTranslateCTM(c, 0, 100);
    putstr(c, "Dash With CGPath Functions", 10, 10);
    buildpath = CGPathCreateMutable();
    CGPathMoveToPoint(buildpath, NULL, 50, 50);
    CGPathAddLineToPoint(buildpath, NULL, 100, 30);
    CGPathAddLineToPoint(buildpath, NULL, 150, 70);
    CGPathAddLineToPoint(buildpath, NULL, 200, 50);
    copy = CGPathCreateCopyByDashingPath(buildpath, NULL, 0, lengths, 2);
    CGContextAddPath(c, copy);
    CGContextStrokePath(c);
    CGContextAddPath(c, copy);
    CGContextSetRGBFillColor(c, 0, 0.25, 0.5, 1);
    CGContextFillPath(c);
    CGPathRelease(copy);
    CGPathRelease((CGPathRef) buildpath);

    CGContextTranslateCTM(c, 0, 100);
    putstr(c, "Dash + Stroke With CGPath Functions", 10, 10);
    buildpath = CGPathCreateMutable();
    CGPathMoveToPoint(buildpath, NULL, 50, 50);
    CGPathAddLineToPoint(buildpath, NULL, 100, 30);
    CGPathAddLineToPoint(buildpath, NULL, 150, 70);
    CGPathAddLineToPoint(buildpath, NULL, 200, 50);
    copy = CGPathCreateCopyByDashingPath(buildpath, NULL, 0, lengths, 2);
    copy2 = CGPathCreateCopyByStrokingPath(copy, NULL, 10, kCGLineCapRound, kCGLineJoinBevel, 10);
    CGContextAddPath(c, copy2);
    CGContextSetRGBFillColor(c, 0, 0.25, 0.5, 1);
    CGContextFillPath(c);
    CGContextAddPath(c, copy2);
    CGContextStrokePath(c);
    CGPathRelease(copy2);
    CGPathRelease(copy);
    CGPathRelease((CGPathRef) buildpath);

    CGContextRestoreGState(c);
}

- (BOOL)isFlipped
{
    return YES;
}

@end

@interface appDelegate : NSObject<NSApplicationDelegate>
@end

@implementation appDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)note
{
    NSWindow *mainwin;
    NSView *contentView;
    dashStrokeView *view;
    NSDictionary *views;
    NSArray *constraints;

    mainwin = [[NSWindow alloc] initWithContentRect: NSMakeRect(0, 0, 320, 360)
        styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask)
        backing:NSBackingStoreBuffered
        defer:YES];
    [mainwin setTitle:@"Dash/Stroke Example"];
    contentView = [mainwin contentView];

    view = [[dashStrokeView alloc] initWithFrame:NSZeroRect];
    [view setTranslatesAutoresizingMaskIntoConstraints:NO];
    [contentView addSubview:view];

    views = NSDictionaryOfVariableBindings(view);
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[view]-|"
        options:0
        metrics:nil
        views:views];
    [contentView addConstraints:constraints];
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[view]-|"
        options:0
        metrics:nil
        views:views];
    [contentView addConstraints:constraints];

    [mainwin cascadeTopLeftFromPoint:NSMakePoint(20, 20)];
    [mainwin makeKeyAndOrderFront:nil];
}

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)app
{
    return YES;
}

@end

int main(void)
{
    NSApplication *app;

    app = [NSApplication sharedApplication];
    [app setActivationPolicy:NSApplicationActivationPolicyRegular];
    [app setDelegate:[appDelegate new]];
    [app run];
    return 0;
}
//2015年10月15日
#进口
@界面dashStrokeView:NSView
@结束
void putstr(CGContextRef c,const char*str,双x,双y)
{
NSFont*sysfont;
CFStringRef字符串;
CTFontRef字体;
CFStringRef键[1];
CFTypeRef值[1];
cfyref属性;
CFAttributedStringRef属性str;
CTLineRef线;
sysfont=[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSRegularControlSize]];
font=(CTFontRef)sysfont;//免费桥
string=CFStringCreateWithCString(kCFAllocatorDefault,
str,kCFStringEncodingUTF8);
键[0]=kCTFontAttributeName;
值[0]=字体;
attrs=CFDictionaryCreate(kCFAllocatorDefault,
键、值、,
1.
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
attrstr=CFAttributedStringCreate(kCFAllocatorDefault,string,attrs);
line=CTLineCreateWithAttributedString(attrstr);
CGContextSetTextPosition(c,x,y);
CTLineDraw(直线,c);
释放(线);
CFRelease(attrstr);
CFRelease(attrs);
释放(字符串);
}
@dashStrokeView的实现
-(void)drawRect:(NSRect)r
{
CGContextRef c;
CGFloat长度[2]={10,13};
cgmutablepathrefbuildpath;
CGPathRef副本,副本2;
c=(CGContextRef)[[NSGraphicsContext currentContext]GraphicsSport];
CGContextSaveGState(c);
putstr(c,“带CGContext函数的破折号+笔划”,10,10);
CGContextMoveToPoint(c,50,50);
CGContextAdd