在iOS中导出超大图像

时间:2018-07-18 04:17:34

标签: ios swift image cgbitmapcontextcreate

我正在开发一个在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

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

0 个答案:

没有答案