我在做一个矢量绘图应用程序的过程中,让我告诉你,这不是一个简单的任务,以正确地做,需要相当多的工作。
有些问题要记住:
- 如果你没有使用矢量图形(CGPaths,例如,是 向量),您将无法删除像素化。例如,UIImage, 只有很多分辨率。
- 为了让你的绘图不会看起来像素化,你要 必须重绘一切。如果你有很多绘图,这可能是一个昂贵的任务要执行。
- 具有良好分辨率,同时放大几乎是不可能的,因为这需要过大的情况下,你的图纸可能会超过设备
我用核芯显卡做我的绘图能力,所以我的方式解决这个问题是通过分配和管理多个CGContext并将它们用作缓冲区。我有一个始终保持在我的缩放级别(比例因子为1)的上下文。这种情况随时都会被吸引,并使得当完全放大时,没有时间花在重绘上,因为它已经完成了。缩放时使用另一个上下文进行绘制。当不缩放时,该上下文将被忽略(因为无论如何都必须根据新的缩放级别重新绘制该上下文)。对于要如何进行我的变焦高水平的算法如下:
- (IBAction)handlePinchGesture:(UIGestureRecognizer *)sender
{
if(sender.state == UIGestureRecognizerStateBegan)
{
//draw an image from the unzoomedContext into my current view
//set the scale transformation of my current view to be equal to "currentZoom", a property of the view that keeps track of the actual zoom level
}
else if(sender.state == UIGestureRecognizerStateChanged)
{
//determine the new zoom level and transform the current view, keeping track in the currentZoom property
//zooming will be pixelated.
}
else if(sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateCancelled)
{
if(currentZoom == 1.0)
{
//you are done because the unzoomedContext image is already drawn into the view!
}
else
{
//you are zoomed in and will have to do special drawing
//perform drawing into your zoomedContext
//scale the zoomedContext
//set the scale of your current view to be equal to 1.0
//draw the zoomedContext into the current view. It will not be pixelated!
//any drawing done while zoomed needs to be "scaled" based on your current zoom and translation amounts and drawn into both contexts
}
}
}
这会变得更加复杂,对我来说,因为我有一个缓冲区额外的缓冲,因为画的我的路径图像要比绘制路径快得多时,有是很多绘图。
在管理多个上下文,调整代码以有效地绘制多个上下文,遵循适当的OOD,基于当前缩放和翻译等缩放新绘图等之间,这是一项艰巨的任务。希望这可以激励你,让你走上正轨,或者你认为摆脱这种像素化是不值得的:)
我试过这个,但是由于内存问题我的应用程序崩溃,随机在多个地方抛出SIGABRT。我写了一个示例来展示我如何使用这个,在实际的应用程序中,绘图真的很大,并且有文本和图标太多http://stackoverflow.com/questions/38191771/draw-zoomable-vector-in-ios-using -coregraphics – anoop4real