簡體   English   中英

在 NSImage 上繪圖

[英]drawing on an NSImage

我正在嘗試在 NSImage 上繪制一個圓圈,然后將其顯示在屏幕上。 我想重復執行此操作,以便在每個新圓圈中都不會顯示舊圓圈。 如果我使用下面的代碼繪制圓圈,但每次我繪制一個新圓圈時,舊圓圈仍然存在。

-(void)drawPointToImage:(float)x y:(float)y
{
    // drawpoint and update
    float diameter1 = 20, x_plot, y_plot; // keep diameter1

    if ( isnan(x) || isnan(y) || x < 0.0001 || y < 0.0001 ) {
        return;
    }

    x_plot = [self xToScreen:x];
    y_plot = [self yToScreen:y];

    NSImage *image = [[NSImage alloc]initWithSize:NSMakeSize(canvasWidth, canvasHeight)]; // start with clean image
    image = [NSImage imageNamed:@"538x598.png"];//orginalImage;

    NSRect myRect1 = NSMakeRect(x_plot, y_plot, diameter1, diameter1);
    NSBezierPath *path1;
    path1 = [NSBezierPath bezierPathWithOvalInRect:myRect1];
    [[NSColor blackColor] set];
    [path1 fill];

    imageView.image = image; // display to screen
}

所以我嘗試了以下操作,但現在根本沒有繪制圓圈。 如何每次在新圖像上畫一個圓圈?

-(void)drawPointToImage:(float)x y:(float)y
{
    // drawpoint and update
    float diameter1 = 20, x_plot, y_plot; // keep diameter1

    if ( isnan(x) || isnan(y) || x < 0.0001 || y < 0.0001 ) {
        return;
    }

    x_plot = [self xToScreen:x];
    y_plot = [self yToScreen:y];

    NSImage *image = [[NSImage alloc]initWithSize:NSMakeSize(canvasWidth, canvasHeight)]; // start with clean image
    image = [NSImage imageNamed:@"538x598.png"];//orginalImage;

    [image lockFocus];

    NSRect myRect1 = NSMakeRect(x_plot, y_plot, diameter1, diameter1);
    NSBezierPath *path1;
    path1 = [NSBezierPath bezierPathWithOvalInRect:myRect1];
    [[NSColor blackColor] set];
    [path1 fill];

    [image unlockFocus];

    imageView.image = image; // display to screen
}

這有效。 由於某種原因,“drawAtPoint”命令不起作用,沒有繪制圖像。 此外,第二次調用時,圖像會向下和向右跳躍約 20 個像素。

-(void)drawRect:(NSRect)rect
{
//    return;
    // drawpoint and update
    float diameter1 = 10, x_plot, y_plot;

    if ( isnan(Cx) || isnan(Cy) || Cx < 0.0001 || Cy < 0.0001 ) {
        return;
    }

    x_plot = [self xToScreen:x]; // x is class-wide
    y_plot = [self yToScreen:y]; // y is class-wide


    NSImage *image;// = [[NSImage alloc]initWithSize:NSMakeSize(cieCanvasWidth, cieCanvasHeight)]; // start with clean image
    image = orginalImage; // start with clean image; orginalImage is class-wide

    [NSGraphicsContext saveGraphicsState];

    [image drawInRect:cieRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
//    [image drawAtPoint:CGPointMake(500,500) fromRect:cieRect operation:NSCompositeSourceOver fraction:1.0];

    // draw point
    NSRect myRect1 = NSMakeRect(x_plot, y_plot, diameter1, diameter1);
    NSBezierPath *path1;
    path1 = [NSBezierPath bezierPathWithOvalInRect:myRect1];
    [[NSColor blackColor] setFill];
    [path1 fill];

    [NSGraphicsContext restoreGraphicsState];

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM