簡體   English   中英

iPhone CALayer圖像數組內容值

[英]iPhone CALayer image array contents values

將圖像加載到圖層的方法很簡單:

CALayer *layer = [[CALayer alloc]init];

layer.contents = (id) [UIImage imageNamed:@"image.png"].CGImage;

然后將圖層作為子圖層添加到視圖中,如下所示:

假設你在視圖中

[self.layer addSublayer:layer];

現在,我想將圖像數組加載為動畫,以便最終將圖像動畫化。

因此,在實際執行動畫之前,我已經測試了以下內容:

[values insertObject:(id)[UIImage imageNamed:path].CGImage atIndex:i];

當然,有一個循環運行,將每個圖像輸入正確的索引...然后我得到一個CGImage ..數組供動畫使用。

我已經打印了這個數組,並看到了:

CGImage 0x17d900

CGImage 0x17f4e0

所以值在那里..而且我沒有任何錯誤..但是我沒有看到圖像...

讓我知道您是否有想法。

這是一個適用於我的項目的代碼片段:

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath: @"contents"];
animation.calculationMode = kCAAnimationDiscrete;
animation.duration = 1.0;
animation.values = values; // NSArray of CGImageRefs
[layer addAnimation: animation forKey: @"contents"];

但是,我在動畫幀和舊版iPhone / iPod上的圖像比較粗糙,導致嚴重的性能問題。 如果遇到這種情況,秘訣是使用預渲染的圖像(IIRC,它們由私有CABackingStore類表示)。 簡而言之,您將制作一個正確大小的CALayer,它使用drawInContext:繪制單個動畫幀,然后循環遍歷動畫幀,在其中向圖層提供幀圖像,將其發送給display並將其內容屬性保存到數組。 只要您不嘗試以任何方式操縱預渲染的圖像,緩存技術都是安全的:基本上,您只需要執行layer1.contents = layer2.contents

除非您確實有性能問題,否則不要浪費時間實施以上內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM