1/12 秒ごとにフレームをロードする MonoTouch アプリケーションがあります。私はopenglではなくUIkitを使用しています。私はUIImage
ビューを持っており、バックグラウンドタスクで画像をロードしています。
すべて問題ありませんが、1 分後 (多かれ少なかれ)、アプリケーションが停止し、トレーサーが「メモリ不足」を示します。
エミュレータでは問題なく動作します。プロファイラーを見ているとメモリが破棄されているようですが、iPadで試してみると.... :(
を使用してメモリを解放しますimage.Dispose()
。メモリには 2 つの画像があり、そのうちの 1 つを表示してから、古いものをリリースしています。Windows Phone にも同じロジックがあり、正常に動作するため、この動作は問題ありません。
backgroundTask を使用せず、メイン スレッドから直接画像をロードしようとしました。それは私にもっと時間を与えます!!! backgroundTask を使用すると、アプリケーションは 30 秒間実行されてから終了します。backgroundTask を使用しない場合、1 分間続きます。
どうしたらいいのかわからない!!!! 誰でも私を助けてもらえますか?
ありがとう!!!
Texture2D.FromStream UIImage.FromFile の単なるラッパーです
これはバックグラウンド ワーカーです。
void LoadTextureInBackground()
{
if (currentMovie == null) return;
DateTime timeOnstart = DateTime.Now;
// Mantenemos 2 texturas en memoria para no cargar sobre el mismo objeto texture.
string fileName = currentMovie.GetFileName();
if (lastFileName == fileName) return;
textureLoaded[currentTexture] = Texture2D.FromStream(Device.GraphicsDevice, fileName);
texture = textureLoaded[currentTexture];
currentTexture = 1 - currentTexture;
lastFileName = fileName;
GC.Collect();
System.Threading.Thread.Sleep(50);
}
これは描画方法です:
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public void Draw(TimeSpan totalTime,TimeSpan elapsedTime)
{
if(currentMovie==null) return;
// Mantenemos 2 texturas en memoria para no cargar sobre el mismo objeto texture.
if (texture == null) return;
int newWidth = (int)(texture.Width*RenderSize);
int newHeight = (int)(texture.Height*RenderSize);
Texture2D drawTexture = texture;
// Al cargar usando Texture2D.FromStream, la textura NO lleva elAlpha premultiplicado
//Device.SpriteBatch.Draw(drawTexture, destination, Color.White);
if(CultTravel.AppDelegate.ImagePng.Image!=drawTexture.Texture)
{
AppDelegate.ImagePng.Image=drawTexture.Texture;
AppDelegate.ImagePng.Frame=new System.Drawing.RectangleF(0,480-newHeight,ImagePng.Image.CGImage.Width,newHeight);
// Si la textura que tengo cargada y lista para mostrar es diferente a la que mostraba, libero la antigua
if (lastTextureDraw!=null && lastTextureDraw != texture)
{
lastTextureDraw.Dispose();
}
lastTextureDraw = texture;
}
注: 問題を解決したところですが、バックグラウンド ワーカーを無効にし、Draw メソッドに読み込みコードを追加し、メイン スレッドに GC.Collect を追加する必要があります。
if (lastTextureDraw!=null && lastTextureDraw != texture)
{
lastTextureDraw.Dispose();
// I MUST ADD THIS TO WORK AND DISABLE THE BACKGROUND TASK!!
GC.Collect();
}