我有一个UIView使用其-[drawRect:]方法绘制的自定义.
问题在于抗锯齿行为非常奇怪,因为黑线水平线或垂直线被绘制得非常模糊.
如果我禁用消除锯齿功能CGContextSetAllowsAntialiasing,则按预期绘制所有内容.
抗锯齿:
替代文字http://dustlab.com/stuff/antialias.png
没有抗锯齿(看起来像AA的预期结果):
alt text http://dustlab.com/stuff/no_antialias.png
线宽正好为1,所有坐标都是整数值.
同样的情况,如果我用绘制一个矩形CGContextStrokeRect,但如果我画一模一样CGRect用UIRectStroke.
我已经实现了以下NSOperation,以绘制N自定义视图
- (void)main {
for (int i=0; i<N; i++) {
<< Alloc and configure customView #i >>
//(customView is a UIView with some drawing code in drawrect)
[delegate.view addSubview:customView];
}
NSLog(@"Operation completed");
}
Run Code Online (Sandbox Code Playgroud)
在我的customView的drawRect方法中
- (void)drawRect {
<<Drawing code>>
NSLog(@"Drawed");
delegate.drawedViews++;
if (delegate.drawedViews==VIEWS_NUMBER) {
[delegate allViewsDrawn];
}
}
Run Code Online (Sandbox Code Playgroud)
因此,代表在绘制所有视图时获取通知.
问题是在"操作完成"日志之后,我需要大约5秒才能看到第一个"绘制"日志.
为什么会这样?一般来说,我应该如何表现出哪一行代码占用了这么多时间呢?
------编辑------
有时候(就像10次中的1次一样)我正在崩溃这样做,因为我不应该addsubview从NSOperation 调用,因为它不是线程安全的.所以我改为
[delegate.view performSelectorOnMainThread:@selector(addSubview:) withObject:customView waitUntilDone:NO];
Run Code Online (Sandbox Code Playgroud)
现在我不再崩溃,但这个过程需要很长时间才能执行!比以前多5倍.
为什么这么慢?
有一个NSImage是模板图像(即[NSImage isTemplate]返回YES)。
当我在 NSImageView 中使用它时,它被正确地绘制为模板图像。
但是,如果我使用 手动绘制它,则会将其drawInRect:fromRect:operation:fraction:绘制为纯黑色图像。
如何手动绘制 NSImage 并仍然获得“模板”效果?
像这样:

我知道这不会起作用NSShadow,把它拉进去drawRect:会很好用.
我想在具有透明背景的屏幕上绘制,以便所有内容(例如打开的应用程序仍然可见)。
在我的自定义 NSWindowController 的 windowDidLoad 中,我有以下内容:
[self.window setOpaque: NO];
[self.window setHasShadow:NO];
[self.window setBackgroundColor:[NSColor clearColor]];
[self.window setStyleMask:NSBorderlessWindowMask];
Run Code Online (Sandbox Code Playgroud)
我的自定义 NSWindow 覆盖 canBecomeKeyWindow
- (BOOL)canBecomeKeyWindow {
return YES;
}
Run Code Online (Sandbox Code Playgroud)
我的视图覆盖了drawRect
- (void)drawRect:(NSRect)rect {
[[NSColor clearColor]set];
NSRectFill(rect);
...
}
Run Code Online (Sandbox Code Playgroud)
问题:尝试在自定义视图内使用鼠标事件进行绘制会导致窗口下方的视图/应用程序接收这些事件。
当我不将 NSWindow styleMask 设置为 NSBorderlessWindowMask 或将自定义视图的背景颜色设置为半透明时,它就可以工作。
[[NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:0.05] set]
Run Code Online (Sandbox Code Playgroud)
如何在完全透明和 NSBorderlessWindowMask 的屏幕上绘图?
新的ios 7手机应用程序有一个收藏夹部分.在该部分中,联系人的姓名显示在填充的圆圈旁边,圆圈内的联系人的初始值.
这是怎么画的?使用drawrect还是已经为此创建了对象?
我有以下代码在UIView中显示标记.标记显示效果很好,一旦我们尝试使用变换进行缩放和缩放UIView,即使在调用setNeedsDisplay之后,第一个绘图仍保持原样.
我的自定义UIView子类具有以下代码
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGFloat w=20.0f;
CGFloat h=8.0f;
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
CGContextClearRect(context,self.bounds);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineCap(context, 2.0);
CGMutablePathRef leftMarker=CGPathCreateMutable();
CGPathMoveToPoint(leftMarker, NULL, 0, 0);
CGPathAddLineToPoint(leftMarker, NULL, w, 0);
CGPathAddLineToPoint(leftMarker,NULL, w, h);
CGPathAddLineToPoint(leftMarker,NULL, h, h);
CGPathAddLineToPoint(leftMarker,NULL,h, w);
CGPathAddLineToPoint(leftMarker,NULL,0, w);
CGPathAddLineToPoint(leftMarker,NULL, 0, 0);
CGContextAddPath(context, leftMarker);
CGContextDrawPath(context, kCGPathFill);
const CGAffineTransform rightMarkerTransform=CGAffineTransformMakeRotateTranslate(DEGREES_TO_RADIANS(90),self.frame.size.width,0);
CGPathRef rightMarker=CGPathCreateCopyByTransformingPath(path, &rightMarkerTransform);
CGContextAddPath(context, rightMarker);
CGContextDrawPath(context, kCGPathFill);
const CGAffineTransform leftMarkerBottomTransform=CGAffineTransformMakeRotateTranslate(DEGREES_TO_RADIANS(270),0,self.frame.size.height);
CGPathRef leftMarkerbottom=CGPathCreateCopyByTransformingPath(path, &leftMarkerBottomTransform);
CGContextAddPath(context, leftMarkerbottom);
CGContextDrawPath(context, kCGPathFill);
const CGAffineTransform rightMarkerBottomTransform=CGAffineTransformMakeRotateTranslate(DEGREES_TO_RADIANS(180),self.frame.size.width,self.frame.size.height);
CGPathRef rightMarkerBottom=CGPathCreateCopyByTransformingPath(path, &rightMarkerBottomTransform);
CGContextAddPath(context, rightMarkerBottom);
CGContextDrawPath(context, kCGPathFill); …Run Code Online (Sandbox Code Playgroud) 自从我们谈论以来programmatically,仪器不在我的考虑之中.
一些参考预先列出:
根据文件,
duration属性提供帧之间的时间量.您可以在应用程序中使用此值来计算显示的帧速率...
所以在我的演示项目中,我将一个displayLink添加到mainRunLoop,用于UITrackingRunLoopMode:
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(screenDidUpdated)];
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:UITrackingRunLoopMode];
Run Code Online (Sandbox Code Playgroud)
并用于duration计算FPS.但出乎我的意料,duration总是0.016667(~FPS 60)无论tableView滚动顺畅与否.
我如何使tableView滞后是添加一行- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath:
NSData *data = [NSData dataWithContentsOfURL:url];
Run Code Online (Sandbox Code Playgroud)
然后我转向displayLink.timestamp,它起作用了.
drawRect:?我的第二个想法是观察drawRect:,我想当tableView滚动时,drawRect:它将被逐帧调用,然后我可以根据drawRect:调用之间的时间差来计算FPS .
但它失败了,导致drawRect:只被召唤一次(而细胞多次).这种方式类似Using CADisplayLink,但也许我选择了错误的位置/方法(如drawRect:)来观察,任何推荐的方法供我观察?
using duration to calculate FPS错了吗?谢谢!
使用核心图形,一个交叉影线(在45度处应用一组平行线)如何跨IOS中的形状填充?样例代码?
(我对与MKMapKit中的MKPolygon一起使用特别感兴趣,但是目前仅尝试使用drawRect来查看是否可以在UIView中使用。因此,用交叉阴影线填充UIView的背景)
我正在寻找有关如何正确实现 Metal 渲染循环的一些指导。我的渲染循环正在从AVPlayer.
这是我当前的实现:
CVDisplayLink查询播放器的频率。AVPlayerItemVideoOutput如果存在新帧,则会将其捕获/保存*作为它将渲染到的CVPixelBufferRef属性。MTKView(此时,先前捕获的视频帧被释放)。MTKView设置是用isPaused和enableSetNeedsDisplayto NO。换句话说,内部定时器的任务MTKView是定期调用其drawRect方法。drawRect首先将新到达的 * 转换CVPixelBuffer为 a MTLTexture,然后发生一堆渲染阶段。presentDrawable结束时调用drawRect。*注意:对 的互斥访问由和CVPixelBufferRef配对控制 。dispatch_semaphore_waitdispatch_semaphore_signal
这是正确的做事方式吗? 尽管偶尔会丢失一些帧,但它看起来性能相当不错。在时间方面,Metal 的 Xcode 分析告诉我,我的MTLCommandBuffer运行时间通常不到 3 毫秒。
话虽如此,我看到了一些替代的可能性:
CVDisplayLink实现并获取其中的框架drawRectMTLTexturedrawRectcommitpresentDrawabledrawRectdrawRect另一个问题: …
drawrect ×10
ios ×4
cocoa ×3
objective-c ×3
iphone ×2
uiview ×2
antialiasing ×1
appkit ×1
avplayer ×1
drawing ×1
frame-rate ×1
gradient ×1
ios7 ×1
macos ×1
metal ×1
metalkit ×1
nsimage ×1
nsoperation ×1
nsview ×1
nswindow ×1