5

我有一个自定义地图视图,它由UIScrollView. 滚动视图的子视图由CATiledLayer. 在这里一切都很好。平移和缩放会加载新的地图图块,一切都运行良好。

我想要做的是捕捉动画视频帧到这个滚动视图。本质上,我想为滚动视图contentOffsetzoomScale.

我知道这个概念是合理的,因为我可以获得私有 API 函数UIGetScreenImage()来以 10 fps 的速度捕获应用程序的屏幕,组合这些图像,并且我可以获得平滑的播放动画并且具有滚动视图动画使用的时序曲线。

当然,我的问题是我不能使用私有 API。仔细阅读 Apple在这里列出的替代方案,给我留下了一个几乎可以说是有效的选择:询问并CALayer从中获取。renderInContextUIGraphicsGetImageFromCurrentImageContext()

不过,这似乎不适用于支持的CATiledLayer视图。捕获的是块状、未缩放的图像,就好像从未加载过更高分辨率的图块一样。这在一定程度上是有道理的,因为CATiledLayer为了性能而引入后台线程并且renderInContext从主线程调用可能无法捕获这些更新。即使我也渲染了平铺层,结果也是相似的presentationLayer

CATiledLayer在包含滚动视图的动画过程中,是否有苹果认可的方法来捕获支持视图的图像?或者在任何时候,就此而言?

4

3 回答 3

1

顺便说一句,如果您renderLayer:inContext:CATiledLayer-backed 视图中正确实施,这是可行的。

于 2013-05-06T18:06:19.403 回答
0

我做了一个快速测试,并在包装​​滚动视图的视图上使用 renderInContext: 似乎有效。你试过吗?

于 2012-02-03T01:59:45.800 回答
0

这段代码对我有用。

- (UIImage *)snapshotImageWithView:(CCTiledImageScrollView *)view
{
// Try our best to approximate the best tile set zoom scale to use
CGFloat tileScale;
if (view.zoomScale >= 0.5) {
    tileScale = 2.0;
}
else if (view.zoomScale >= 0.25) {
    tileScale = 1.0;
}
else {
    tileScale = 0.5;
}

// Calculate the context translation based on how far zoomed in or out.
CGFloat translationX = -view.contentOffset.x;
CGFloat translationY = -view.contentOffset.y;
if (view.contentSize.width < CGRectGetWidth(view.bounds)) {
    CGFloat deltaX = (CGRectGetWidth(view.bounds) - view.contentSize.width) / 2.0;
    translationX += deltaX;
}
if (view.contentSize.height < CGRectGetHeight(view.bounds)) {
    CGFloat deltaY = (CGRectGetHeight(view.bounds) - view.contentSize.height) / 2.0;
    translationY += deltaY;
}

// Pass the tileScale to the context because that will be the scale used in drawRect by your CATiledLayer backed UIView
UIGraphicsBeginImageContextWithOptions(CGSizeMake(CGRectGetWidth(view.bounds) / view.zoomScale, CGRectGetHeight(view.bounds) / view.zoomScale), NO, tileScale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, translationX / view.zoomScale, translationY / view.zoomScale);

// The zoomView is a subview of UIScrollView. The CATiledLayer backed UIView is a subview of the zoomView.
[view.zoomView.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image;

}

在这里找到完整的示例代码:https ://github.com/gortega56/CCCanvasView

于 2015-08-25T12:39:53.967 回答