1

私のアプリケーションはARCにあります。
imageView で画像アニメーションを使用していますが、しばらくするとアプリケーションがメモリ不足エラーでクラッシュしました。

ログにはエラー メッセージが表示されませんが、プロファイル ツールでは割り当て部分 に「メモリ不足」というメッセージが表示されます。

私のアニメーションの1つのコードは

ImageView= [[UIImageView alloc] init];
[self.view addSubview:ImageView];
ImageView.frame=CGRectMake(0, 0, 480, 342);
ImageView.animationImages = [NSArray arrayWithObjects:
                                  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image1" ofType:@"png"]],
                                  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image2" ofType:@"png"]],
                                  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image3" ofType:@"png"]],
                                  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image4" ofType:@"png"]],
                                  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image5" ofType:@"png"]],
                                  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image6" ofType:@"png"]],
                                  nil];
[ImageView setAnimationRepeatCount:1];
ImageView.animationDuration =1.0;
[ImageView startAnimating];

このアニメーション コードにコメントすると、アプリケーションは正しく実行されますが、1 つのビューにこのタイプのアニメーションが 5 つ以上必要です。

リンク、チュートリアル、アイデアは非常に役立ちます...

4

2 に答える 2

0

Gerharbo の方法は、基本的にテクスチャ アトラスですが、1 つのはるかに大きな画像を使用します。多くの画像を (長いアニメーションで) 使用しようとすると、この種のアプローチは適切にスケーリングされないことがわかると思います。メモリ不足によるクラッシュが既に発生しているため、UIImageView.animationImages を完全に回避することをお勧めします。このブログ投稿video-and-memory-usage-on-ios-devicesでは、メイン メモリに一連の画像を保持するためにメモリを割り当てることができない理由について説明しています。SO を見回すと、他の多くの開発者がこの同じ問題に遭遇していることがわかります。

于 2013-06-21T04:30:14.770 に答える
0

スプライトで試してみませんか?CALater (Core Animation Layer) でアニメーション化しようとしましたが、魅力的に機能します。「contentsRect」をアニメーション化します。短いコード例 (CALayer サブクラス):

self.contents = [UIImage imageNamed:@"sprite-large.png"].CGImage;

NSMutableArray *frames = [NSMutableArray array]; // array of CGRect's
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"contentsRect"];
anim.values = frames;
[self addAnimation:anim forKey:nil];

数週間前に GitHub にいくつかのコードを投稿しました: https://github.com/Gerharbo/sprite-animation-layer/

スプライトの例 (完全ではありませんが、機能します) とサンプル コードを提供します。

アニメーションを楽しんでください!

ゲルハルト

于 2013-05-02T20:54:30.763 に答える