私は私が働いているiPhoneアプリケーションで "スプライト"として私たちにCALayersを試みてきました。私が読んできたことから、それはここで正しいことのように思えます。UIViewクラスの外でCALayerを初期化できない理由はありますか?
UIViewクラス内でレイヤーを設定すると、レイヤーを画面に表示させることができます。 NSLog(@ "GameView initWithCoder");
私はUIViewの外のCALayerを初期化しようとしたときしかし、私のアプリケーションがクラッシュしNSString* imagePath = [[NSBundle mainBundle] pathForResource:@"earth" ofType:@"png"];
earthImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
earthLayer = [CALayer layer];
[earthLayer setContents:(id)[earthImage CGImage]];
[earthLayer setBounds:CGRectMake(0, 0, 32, 32)];
[earthLayer setPosition:CGPointMake(50, 50)];
[earthLayer setName:@"earth"];
[[self layer] addSublayer:earthLayer];
:
- (void)loadImage:(NSString*)fileName
{
NSLog(@"GamePiece loadImage");
NSArray* fileNameParts = [fileName componentsSeparatedByString:@"."];
imageName = [[fileNameParts objectAtIndex:0] retain];
NSString* ext = [fileNameParts objectAtIndex:1];
NSString* imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:ext];
image = [[UIImage alloc] initWithContentsOfFile:imagePath];
layer = [CALayer layer];
[layer setContents:(id)[image CGImage]];
[layer setBounds:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
[layer setPosition:CGPointMake(50.0f, 50.0f)];
[layer setName:imageName];
[fileNameParts release], fileNameParts = nil;
[ext release], ext = nil;
}
NSLog(@"GameView initWithCoder");
earth = [[GamePiece alloc] init];
[earth loadImage:@"earth.png"];
[[self layer] addSublayer:[earth layer]];
は、明らかに私はここで何か間違ったことをやっているが、私はちょうどドンそれが何であるかは分かりません。私はCore Animation Programming GuideとiPhone Application Programming Guideを読んだことがありますが、実際にはCALayerを初期化する方法以外については話していません。
ちょうど正しい方向に穏やかなナッジが必要です。 iPhoneをプログラミングするときに私が学んだ他のすべてについて、かなり理解しています。グラフィックス、レイヤー、アニメーションに問題があります。
ありがとうございます!
実際には、これまでのところラッキーだったとしても、レイヤーはまだ問題です。あなたはそれがivarの場合はそれを保持する必要があります。ファイル名については、私はそれを逃した。実際にそれを保持またはコピーする必要はありません。ちょうどそれを解放しないでください:)基本的に、あなたがあなたがそれらを作成しない限り(alloc/copy/new)あなたの関数の期間中にのみ使用するものは積極的に管理する必要はありません。あなたが抱きしめたいものは積極的に管理する必要があります(割り当てない/コピーしていない場合は、保持する必要があります)。 –