using System; using System.Net; using CoreGraphics; using UIKit; namespace Shared { public class UrlMonitor : View { public UrlMonitor() { uiView = new UIImageView { Tag=int.MinValue}; } public //是否已经启动 bool isStarted; /// /// 用户名 /// public string UserName = ""; /// /// 密码 /// public string Password = ""; /// /// 摄像头的图片地址 /// public string URL; /// /// 启动 /// public void Start() { if (isStarted) { return; } isStarted = true; System.Threading.Tasks.Task.Run(() => { while (isStarted) { System.Threading.Thread.Sleep(100); try { WebClient webClient = new WebClient(); webClient.Headers.Add("Accept-Encoding", "gzip, deflate"); webClient.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(UserName + ":" + Password))); var bytes = webClient.DownloadData(URL); var uiImage = UIImage.LoadFromData(Foundation.NSData.FromArray(bytes)); Application.RunOnMainThread(() => { try { (uiView as UIImageView).Image = imimage(uiImage); } catch { } }); } catch { } } }); } /// /// 停止播放 /// public void Stop() { isStarted = false; } /// /// 旋转当前画面 /// /// 源图 /// 旋转后的界面 UIImage imimage(UIImage image) { if (Application.IsPad) { return image; } else { var rotate = 3 * Math.PI / 2; var rect = new CGRect(0, 0, image.Size.Height, image.Size.Width); nfloat translateX = -rect.Size.Height; nfloat translateY = 0; nfloat scaleX = rect.Size.Height / rect.Size.Width; nfloat scaleY = rect.Size.Width / rect.Size.Height; //case UIImageOrientation.Right: //rotate = 3 * Math.PI / 2; // rect = new CGRect(0, 0, image.Size.Height, image.Size.Width); // translateX = -rect.Size.Height; // translateY = 0; // scaleY = rect.Size.Width / rect.Size.Height; // scaleX = rect.Size.Height / rect.Size.Width; // break; //case UIImageOrientation.Down: // rotate = M_PI; // rect = new CGRect(0, 0, image.Size.Width, image.Size.Height); // translateX = -rect.Size.Width; // translateY = -rect.Size.Height; // break; //default: // rotate = 0.0f; // rect = new CGRect(0, 0, image.Size.Width, image.Size.Height); // translateX = 0; // translateY = 0; // break; UIGraphics.BeginImageContext(rect.Size); var context = UIGraphics.GetCurrentContext(); //做CTM变换 context.TranslateCTM(0.0f, rect.Size.Height); context.ScaleCTM(1.0f, -1.0f); context.RotateCTM((nfloat)rotate); context.TranslateCTM(translateX, translateY); context.ScaleCTM(scaleX, scaleY); //绘制图片 context.DrawImage(new CGRect(0, 0, rect.Size.Width, rect.Size.Height), image.CGImage); return UIGraphics.GetImageFromCurrentImageContext(); } } } }