1

我正在开发一个应用程序,它在 UIScrollView 中使用 CATiledLayer 支持的视图,让用户在任意大的画布上工作。画布基本上包含用户可以着色、选择、移动等的大块图表。我覆盖draw(_ rect: CGRect)以在视图上绘制。

所有这些都运行良好 - CATiledLayer 在用户处理图表时可以很好地降低内存消耗。滚动和缩放效果很好。

我现在需要做的是允许用户将他们的图表副本导出到相机胶卷,但我遇到了内存问题并且应用程序崩溃了。我尝试了多种方法来将视图捕获到图像中。该视图在 UIScrollView 中并不完全可见

这是我当前生成图像的方式:

// MyView is the view that is used in the scrollView
static func screenshotByDrawingItAgain(_ view: MyView) -> UIImage? {
    var image: UIImage?

    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, UIScreen.main.scale)

    guard let context = UIGraphicsGetCurrentContext() else {
        return nil
    }
    // This is where my chart data model is stored in memory
    guard let chart = view.chart else {
        return nil
    }

    // drawInRect is also called from MyView's draw(_:) method
    // so all drawing code is in one place
    view.drawInRect(context, with: chart, in: view.bounds, row: 0, col: 0)
    image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return image!
}

由于图表可以是任意大小,这意味着它们可以变得非常大。我需要一种能够以某种方式导出像这样的大图像的方法。示例错误:

BOUNDS (10200.0, 6360.0)
CGBitmapContextInfoCreate: unable to allocate 1037952000 bytes for bitmap data

我看到的大多数帖子都涉及如何阅读大图像(如照片)并显示它。我无法找到任何有关如何进行反向操作并获取视图图像的信息。有没有一种方法可以将视图的较小图块生成到文件上,然后将其拼接在一起并让他们访问生成的文件?谢谢!

4

0 回答 0