我正在尝试使用单声道触控为 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 秒后收到第二个警告,然后应用程序崩溃。
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。是否有一些需要调用的方法或者有更好的解决我的问题的方法?任何帮助,将不胜感激。
谢谢