2012-04-06 24 views
0

View Programming Guide for iOS on pg。 57有這樣一段代碼:這個CALayer設置代碼有什麼問題

// Create the layer. 
CALayer* myLayer = [[CALayer alloc] init]; 
// Set the contents of the layer to a fixed image. And set 
// the size of the layer to match the image size. 
UIImage layerContents = [[UIImage imageNamed:@"myImage"] retain]; 
CGSize imageSize = layerContents.size; 
myLayer.bounds = CGRectMake(0, 0, imageSize.width, imageSize.height); 
myLayer = layerContents.CGImage; 

你爲什麼會想這樣做的最後一行:myLayer = layerContents.CGImage;。首先,對象不匹配,所以這是一個類型問題。但它取代了第一個語句中創建的對象!

我錯過了什麼?

回答

2

看起來像那個文件是錯誤的。它應該是:

myLayer.contents = (id)layerContents.CGImage; 

這是一種設置圖層內容的方法。查看docs for contents