是否有可能在單個CALayer上繪製具有自定義繪圖的幾個UIViews,以便它們每個都沒有後備存儲?在一個圖層上繪製幾個UIViews
UPDATE:
我有幾個大小相同的uiviews具有相同的上海華。現在他們每個人都有自定義繪圖。而且由於尺寸較大,他們在iPad 3上創建了600-800 MB的後備商店。所以我想在一個視圖中編寫它們的輸出,並且消耗的內存要少幾倍。
是否有可能在單個CALayer上繪製具有自定義繪圖的幾個UIViews,以便它們每個都沒有後備存儲?在一個圖層上繪製幾個UIViews
UPDATE:
我有幾個大小相同的uiviews具有相同的上海華。現在他們每個人都有自定義繪圖。而且由於尺寸較大,他們在iPad 3上創建了600-800 MB的後備商店。所以我想在一個視圖中編寫它們的輸出,並且消耗的內存要少幾倍。
每個視圖都有自己的圖層,您無法更改該圖層。
您可以啓用shouldRasterize
以平鋪視圖層次結構,這在某些情況下可能有所幫助,但需要gpu內存。
另一種方式可以是創建圖像上下文並將圖形合併到圖像中並將其設置爲圖層內容。
在去年的wwdc會議視頻之一是關於繪圖顯示一個繪圖應用程序,其中很多筆畫被轉移到圖像加速繪圖。
實際上,這就是我現在要做的 - 使用圖像上下文。 –
由於視圖將共享相同的後備存儲,因此我假設您希望它們共享圖層自定義繪圖所產生的相同圖像,對不對?我相信這可以用類似於:
// 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];
我會說沒有,因爲當一個CALayer附加到一個視圖,它需要該視圖設置爲圖層的代表。由於委託在Cocoa設計中是一對一的關係,因此對象(如CALayers)不能同時擁有多個委託,因此將它們同時用於在多個視圖中顯示是不太可能的。 – 2012-10-18 21:57:12