nop*_*ole 14 iphone core-graphics uikit ios
在iOS上,我们可以在drawRect
使用中画一条线
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath (context);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, 100, 100);
CGContextStrokePath(context);
Run Code Online (Sandbox Code Playgroud)
但是如果我们删除上面的代码,我们也可以绘制一个矩形,并使用:
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 100, 100)];
[path stroke];
Run Code Online (Sandbox Code Playgroud)
两个相关问题:
1)为什么UIBezierPath
不需要获取或使用当前上下文?
2)如果我有两个上下文:一个用于屏幕,一个是位图上下文,那么如何判断要绘制的上下文怎么UIBezierPath
办?我认为它可能是UIGraphicsSetCurrentContext
但它不存在.
Lil*_*ard 22
UIBezierPath
确实使用了上下文.它使用当前的UIKit图形上下文.这与你已经获得的完全相同UIGraphicsGetCurrentContext()
.
如果您想UIBezierPath
使用不同的上下文,可以使用UIGraphicsPushContext()
,但必须记住UIGraphicsPopContext()
在完成后使用.
我认为提到CGContextFillRect比使用UIBezierPath要快8.5倍(如果性能是一个因素并假设您不需要使用UIBezierPath进行更复杂的绘制)可能会有用.
我在Apple的HazardMap示例中添加了一些时间(http://developer.apple.com/library/ios/#samplecode/HazardMap/Introduction/Intro.html),对于CGContextFillRect,以mm为单位的时间为~0.00064 ms/rect对于UIBezierPath方法,方法与~0.00543 ms/rect相比,可能是b/c后者需要更多的消息传递开销.
即我正在比较使用
CGContextFillRect(ctx, boundaryCGRect);
Run Code Online (Sandbox Code Playgroud)
与使用
UIBezierPath* path = [UIBezierPath bezierPathWithRect:boundaryCGRect];
[path fill];
Run Code Online (Sandbox Code Playgroud)
在HazardMapView的内部循环中(加上上面提到的更改来推送/弹出传递给HazardMapView的上下文drawMapRect:zoomScale:inContext :).
ETA
在iOS上,我们可以在
drawRect
使用中画一条线
我已经强调了本声明的重要部分.在内部drawRect:
,UIKit已经为您设置了一个上下文,任何基于对象的绘图指令都直接进入该上下文.UIBezierPath
确实在使用该上下文,它不需要显式传递.
在Cocoa Touch中,必须始终存在绘图上下文(在这种情况下,上下文最终会被绘制到屏幕上).如果你不在里面drawRect:
,你必须自己创建一个上下文.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath (context);
CGContextMoveToPoint(context, 0, 0);
Run Code Online (Sandbox Code Playgroud)
请注意,第一个函数调用是GetCurrentContext()
.当你使用CoreGraphics的功能绘图界面时,你需要将一个上下文传递给每个函数,但你不是在这里创建一个,你只是检索已经存在的那个.
图形上下文在堆栈中.如果你想绘制你创建的上下文,你可以使用UIGraphicsPushContext()
(如Kevin已经提到的那样)将它推入堆栈,然后回到上一个.
归档时间: |
|
查看次数: |
5869 次 |
最近记录: |