1

単一のCALayerにカスタム描画を使用して複数のUIViewを描画し、それぞれにバッキングストアがないようにすることは可能ですか?

アップデート:

同じスーパービューを持つ同じサイズのuiviewがいくつかあります。現在、それぞれにカスタム描画があります。また、サイズが大きいため、iPad 3で600〜800 mbのバッキングストアを作成します。そのため、1つのビューで出力を作成し、メモリの消費量を数分の1に減らしたいと考えています。

4

2 に答える 2

3

すべてのビューには独自のレイヤーがあり、それを変更することはできません。

ビュー階層をフラット化できるようにすることもできshouldRasterizeます。これは場合によっては役立つかもしれませんが、GPUメモリが必要です。

別の方法は、画像コンテキストを作成し、図面を画像にマージして、それをレイヤーコンテンツとして設定することです。

昨年の描画に関するwwdcセッションビデオの1つで、描画を高速化するために多くのストロークが画像に転送される描画アプリが示されました。

于 2012-10-18T22:08:43.530 に答える
1

ビューは同じバッキングストアを共有するので、レイヤーのカスタム描画から得られた同じ画像を共有してほしいと思いますよね?これは、次のような方法で実行できると思います。

// create your custom layer
MyCustomLayer* layer = [[MyCustomLayer alloc] init];

// create the custom views
UIView* view1 = [[UIView alloc] initWithFrame:CGRectMake( 0, 0, layer.frame.size.width, layer.frame.size.height)];
UIView* view2 = [[UIView alloc] initWithFrame:CGRectMake( 100, 100, layer.frame.size.width, layer.frame.size.height)];

// have the layer render itself into an image context
UIGraphicsBeginImageContext( layer.frame.size );
CGContextRef context = UIGraphicsGetCurrentContext();
[layer drawInContext:context];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// set the backing stores (a.k.a the 'contents' property) of the view layers to the resulting image
view1.layer.contents = (id)image.CGImage;
view2.layer.contents = (id)image.CGImage;

// assuming we're in a view controller, all those views to the hierarchy
[self.view addSubview:view1];
[self.view addSubview:view2];
于 2012-10-19T01:15:02.457 に答える