使用 Core Graphics 重绘时如何消除 Objective-C 图像伪影

发布于 2024-12-29 17:55:11 字数 1212 浏览 0 评论 0原文

我使用基本的 NSBezierPath 方法在屏幕上绘制矩形。我设置了一个对象,在屏幕上绘制一个自定义的矩形,并初始化该对象的每个实例。在该对象中,我有一个方法,当将矩形拖动到屏幕上时,用鼠标移动矩形。为了实现这一点,我简单地使用了 mouseDragged: 方法。问题是,当我移动它时,它会留下“图像伪影”,这些伪影在屏幕上沿着矩形先前所在位置的边缘显示为 1px 细线。这类似于当 Microsoft Windows 运行缓慢时,当您拖动窗口时,它会留下窗口的重影。还记得您总是在解冻之前尝试绘制东西或填充整个屏幕吗?就像它重画得不够快什么的。这些伪像永远不会消失,除非我通过在它们上面绘制一些东西(例如将矩形拖回到它们上面)来清除它们。

-(void)mouseDown:(NSEvent *)theEvent{
mouseLoc = [theEvent locationInWindow];
xDifference = mouseLoc.x - origin.x;
yDifference = mouseLoc.y - origin.y;

}

-(void)mouseDragged:(NSEvent *)theEvent{
mouseLoc = [theEvent locationInWindow];
NSPoint adjustedOrigin = NSMakePoint((mouseLoc.x-xDifference), (mouseLoc.y-yDifference));
[self setOrigin:adjustedOrigin];


}

另外,如果您想知道蓝色矩形左侧的空白方块,我也无法解释。我有一个黑色的“菱形”绕着直径 30px 的圆圈转,但每次它移动一格(在 NSTimer 上每秒一格),它都会非常短暂地跳到那个位置,但为什么我不知道。矩形移动的代码如下:

i+=.5;
int rectangle2X = (100+30*cos(i));
int rectangle2Y = (100+30*sin(i));
//NSLog(@"(%d, %d)", rectangle2X, rectangle2Y);
rectangle2.origin = NSMakePoint(rectangle2X, rectangle2Y);

任何帮助都会很棒。我知道上面的代码并不是你们可能需要的全部内容,所以请随意询问更多。顶部代码来自对象,底部代码来自AppDelegate。图片中的两个矩形都是我的对象的实例。

文物图片

I'm drawing rectangles on the screen using the basic NSBezierPath method. I have an object set up to draw a customized rectangle on the screen with each instance of the object initialized. In that object I have a method that moves the rectangle around with the mouse when its dragged across the screen. To accomplish this I simply used the mouseDragged: method. The problem is as I move it, it leaves behind "image artifacts" which appear as thin 1px lines on the screen along the edges of where the rectangle was previously. This is similar to how when Microsoft Windows was running slowly, it used to leave ghosts of a window as you dragged it around. Remember how you would always try to draw stuff or fill the entire screen before it unfroze? Its like its just not redrawing quickly enough or something. The artifacts never go away unless I clear them out by drawing something over them (like dragging the rectangle back over them).

-(void)mouseDown:(NSEvent *)theEvent{
mouseLoc = [theEvent locationInWindow];
xDifference = mouseLoc.x - origin.x;
yDifference = mouseLoc.y - origin.y;

}

-(void)mouseDragged:(NSEvent *)theEvent{
mouseLoc = [theEvent locationInWindow];
NSPoint adjustedOrigin = NSMakePoint((mouseLoc.x-xDifference), (mouseLoc.y-yDifference));
[self setOrigin:adjustedOrigin];


}

Also if you're wondering about the blank square to the left of the blue rectangle, I can't explain it either. I have the black 'rhombus' going around in circles 30px in diameter, but every time it moves one space (one a second on an NSTimer) it jumps very briefly to that spot, but why I have no clue. the code for the movement of the rectangle is below:

i+=.5;
int rectangle2X = (100+30*cos(i));
int rectangle2Y = (100+30*sin(i));
//NSLog(@"(%d, %d)", rectangle2X, rectangle2Y);
rectangle2.origin = NSMakePoint(rectangle2X, rectangle2Y);

Any help would be great. I know the code above isn't everything that you guys might need so feel free to ask for more. The top code is from the object, the bottom code comes from the AppDelegate. Both rectangles in the picture are instances of my object.

Picture of Artifacts

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

下雨或天晴 2025-01-05 17:55:11

别担心,就称之为现代艺术吧!

但说真的。看起来您没有在 drawRect:drawInContext: 方法中清除视图。

在边界上绘制一个背景色矩形,作为清除先前绘图的第一件事。

[myBGColor set];
NSRectFill([self bounds]);

NSView 不会自动清除。

Don't worry just call it modern art!

But seriously. It looks like you are not clearing the view in the drawRect: or drawInContext: method.

Paint a rect of background color over the bounds as the first thing you do to clear the prior drawing.

[myBGColor set];
NSRectFill([self bounds]);

NSViews don't automatically clear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文