Quartz2D如何保存当前上下文然后在其上绘制?

发布于 2024-10-14 18:49:02 字数 111 浏览 5 评论 0原文

我正在尝试使用 Quartz2D 进行绘图,但我发现绘图在几个点之后滞后很多。因此我想知道如何将当前上下文缓存到 bmp 或 jpg 或图层中,然后再次绘制到图层上以使绘图更平滑。哪种方法是最好的方法?谢谢!

I'm trying to draw using Quartz2D and but i'm finding the drawing to lag a lot right after a few dots. Therefore I am wondering how can I cache the current context into a bmp or a jpg or a layer and then draw onto the layer again to make the drawing smoother. Which way is the best way to do it? Thanks!

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

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

发布评论

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

评论(3

情归归情 2024-10-21 18:49:02

您可能想尝试在后台线程中绘制位图,而不是在 UIView 的 drawRect 中绘制并尝试保存该绘制层。您永远不必清除位图,只需继续在其中绘制即可。然后,您在 UIView drawRect 中所需要做的就是绘制最新可用位图的一个图像,iOS 可以相当快地完成此操作。

Instead of drawing in a UIView's drawRect and trying to save that drawing layer, you might want to try drawing into a bitmap in a background thread. You never have to clear the bitmap and can just keep drawing into it. Then all you have to do in the UIView drawRect is one image draw of the latest bitmap available, which iOS can do fairly quickly.

━╋う一瞬間旳綻放 2024-10-21 18:49:02

IIRC,Quartz2D 使用绘画模型来渲染内容,一旦你绘制了一些东西,你就根本无法与它交互,所以我不明白为什么缓存当前上下文会有任何帮助。

但是,如果您想缓存当前上下文,代码如下所示:

// Begin image context
UIGraphicsBeginImageContext(view.bounds.size);

[view.layer renderInContext:UIGraphicsGetCurrentContext()];
CGImageRef screenshotRef = CGBitmapContextCreateImage(UIGraphicsGetCurrentContext());
UIImage *screenshot = [[UIImage alloc] initWithCGImage:screenshotRef];

// End image context
UIGraphicsEndImageContext();
// Release CGImage
CGImageRelease(screenshotRef);

return screenshot;

IIRC, Quartz2D uses a painting model to render content, and once you draw something you can't interact with it at all, so I don't see why caching the current context would do anything to help.

However, if you want to cache your current context, the code looks something like this:

// Begin image context
UIGraphicsBeginImageContext(view.bounds.size);

[view.layer renderInContext:UIGraphicsGetCurrentContext()];
CGImageRef screenshotRef = CGBitmapContextCreateImage(UIGraphicsGetCurrentContext());
UIImage *screenshot = [[UIImage alloc] initWithCGImage:screenshotRef];

// End image context
UIGraphicsEndImageContext();
// Release CGImage
CGImageRelease(screenshotRef);

return screenshot;
不交电费瞎发啥光 2024-10-21 18:49:02

您可以通过首先将不同图层转换为图像并聚合为单个单元来将一个图层绘制到另一图层。

    //Draw path and return Image
    let firstBeizerPath:UIImage = drawPath(firstBeizerpath: drawCircle())
    let secondBeizerPath:UIImage = drawPath(firstBeizerpath: polygon())

    //Draw different saved context into single image.
    let image = UIImage(). drawOneLayerOverAnother(firstImage:firstBeizerPath , secondImage:secondBeizerPath)

extension UIImage {
    func drawOneLayerOverAnother(firstImage:UIImage , secondImage:UIImage ) -> UIImage {
    UIGraphicsBeginImageContext(firstLayer.size)

    firstImage.draw(in: CGRect(x: 0, y: 0, width: firstLayer.size.width, height: firstLayer.size.height) )
    secondImage.draw(in: CGRect(x: 0, y: 0, width: firstLayer.size.width, height: firstLayer.size.height) )

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage!
    }
}

演示应用

You can draw one layer to another layer by first converting different layers to images and converge into single Unit.

    //Draw path and return Image
    let firstBeizerPath:UIImage = drawPath(firstBeizerpath: drawCircle())
    let secondBeizerPath:UIImage = drawPath(firstBeizerpath: polygon())

    //Draw different saved context into single image.
    let image = UIImage(). drawOneLayerOverAnother(firstImage:firstBeizerPath , secondImage:secondBeizerPath)

extension UIImage {
    func drawOneLayerOverAnother(firstImage:UIImage , secondImage:UIImage ) -> UIImage {
    UIGraphicsBeginImageContext(firstLayer.size)

    firstImage.draw(in: CGRect(x: 0, y: 0, width: firstLayer.size.width, height: firstLayer.size.height) )
    secondImage.draw(in: CGRect(x: 0, y: 0, width: firstLayer.size.width, height: firstLayer.size.height) )

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage!
    }
}

Demo App

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