1

モノタッチを使ってiPhone用のIPカメラアプリケーションを作成しようとしています。カメラフィードを取得するには、CGImage.ScreenImageメソッドを継続的に呼び出し、結果のUIImageをバイト配列に変換してからブラウザーに送信します。カメラフィードは、UIImagePickerControllerクラスを使用して、

SourceType = UIImagePickerControllerSourceType.Camera;

ImageControllerクラスの完全なコードは次のとおりです。

public class ImageController : UIImagePickerController
{
    private HttpListener listener;

    public ImageController ()
    {
        SourceType = UIImagePickerControllerSourceType.Camera;
        start();
    }

    void start()
    {
        listener = new HttpListener();
        listener.Prefixes.Add( "http://+:8001/" );
        listener.Prefixes.Add( "http://+:8001/current.jpg/" );
        listener.Start();
        listener.BeginGetContext( HandleRequest, listener );
    }

    public override void DidReceiveMemoryWarning ()
    {
        GC.Collect();
    }


    private void HandleRequest(IAsyncResult result)
    {
        HttpListenerContext context = listener.EndGetContext( result );
        listener.BeginGetContext( HandleRequest, listener );
        if ( context.Request.RawUrl.Contains( "current.jpg" ) )
        {
            context.Response.StatusCode = (int) HttpStatusCode.OK;
            context.Response.ContentType = @"multipart/x-mixed-replace;boundary=--myboundary";
            context.Response.KeepAlive = true;
            byte [] imageData;
            byte [] boundary;

            while ( true )
            {
                imageData = worker();
                boundary =
                    Encoding.UTF8.GetBytes(
                        "\r\n--myboundary\r\nContent-Type: image/jpeg\r\nContent-Length: " +
                        imageData.Length + "\r\n\r\n" );
                context.Response.OutputStream.Write( boundary, 0, boundary.Length );
                context.Response.OutputStream.Write( imageData, 0, imageData.Length );
                imageData = null;
                boundary = null;
            }
        }
        else
        {
            string responseString = @"<html xmlns=""http://www.w3.org/1999/xhtml""> <body bgcolor=""#fffffff"">"+
                        @"<img name=""current"" src=""current.jpg"" border=""0"" id=""current"" /></body></html>";

            byte [] responseByte = Encoding.UTF8.GetBytes( responseString );
            context.Response.ContentType = "text/html";
            context.Response.KeepAlive = true;
            context.Response.StatusCode = (int) HttpStatusCode.OK;
            context.Response.ContentLength64 = responseByte.Length;
            context.Response.OutputStream.Write( responseByte, 0, responseByte.Length );
            context.Response.OutputStream.Close();
         }
     }

     private byte [] worker()
     {
         byte [] imageArray;
         using ( var screenImage = CGImage.ScreenImage )
         {
             using ( var image = UIImage.FromImage(screenImage) )
             {
                 using ( NSData imageData = image.AsJPEG())
                 {
                     imageArray = new byte[imageData.Length];
                     Marshal.Copy(imageData.Bytes, imageArray, 0, Convert.ToInt32(imageData.Length));
                 }
             }
         }
         return imageArray;
      }
   }
}

私が抱えている問題は、ストリーミングの約20秒後にメモリ警告を受け取り、5秒後に2番目の警告を受け取り、アプリケーションがクラッシュすることです。

2010-11-08 13:12:56.457 MonoTouchCGImageScreenImageTest [2251:307]メモリ警告を受け取りました。レベル=1

2010-11-08 13:13:11.059 MonoTouchCGImageScreenImageTest [2251:307]メモリ警告を受け取りました。レベル=2

表示されたすべての画像はusingステートメント内で破棄され、imageData変数も送信されるとnullに設定されます。呼び出す必要のあるメソッドはありますか、それとも私の問題に対するより良い解決策はありますか?どんな助けでもいただければ幸いです。

ありがとう

4

1 に答える 1

4

無限ループでworker()を呼び出しています。

        while ( true )
        {
            imageData = worker();
            boundary =
                Encoding.UTF8.GetBytes(
                    "\r\n--myboundary\r\nContent-Type: image/jpeg\r\nContent-Length: " +
                    imageData.Length + "\r\n\r\n" );
            context.Response.OutputStream.Write( boundary, 0, boundary.Length );
            context.Response.OutputStream.Write( imageData, 0, imageData.Length );
            imageData = null;
            boundary = null;
        }

つまり、廃棄している場合でも、割り当てのループがタイトになり、GCに大きなプレッシャーがかかることになります。

さらに、obj-cランタイムから自動解放されたオブジェクトを返すUIImage.FromImage()を使用していますが、メインループに譲ることがないため、NSAutoreleasePoolはこのオブジェクトを解放できません。

于 2010-11-08T15:51:59.597 に答える