Xamarin.iOS(百分比进度条)网络图片加载

来源:互联网 发布:linux oracle监听配置 编辑:程序博客网 时间:2024/05/07 17:16

    SDWebImage本是ObjC的一个开源控件,在gitub上有对Xamarin的完整binding封装,同时还将其支持UIImageView/UIButton等控件的扩展方法,参考博文的功能说明(SDWebImage):

    功能

   1.为UIImageView、UIButton加载网络图片,为Cocoa Touch框架提供缓存管理。

   2.异步图片下载

   3.异步内存+磁盘图片缓存,自动缓存过期处理。

   4.确保同一个url不会被加载很多次

   5.确保虚假url不会被重复提交很多次

   简单来说就是其能够对网络图片进行内存管理及本地存储管理,并且能够手工清除内存及本地缓存。


   SDWebImage单独使用

   1.WebCahce>>UIImageView/UIButton图片加载(可设置默认图片及完成回调)

partial void ImageButton_TouchUpInside (UIButton sender){this.LoadingView.StartAnimating();string url = "http://www.51ppt.com.cn/Article/Uploadphotos_0708/200604/200641473458360.png";this.ImageButton.SetImage(url,UIControlState.Normal,null,SDWebImageOptions.None,(img,error,cache)=>{this.LoadingView.StopAnimating();});}

private void LoadClick(){<span style="white-space:pre"></span>string url = "http://d.hiphotos.baidu.com/image/pic/item/8cb1cb1349540923b6a062619058d109b2de49e7.jpg";this.LoadingView.StartAnimating ();this.webImageView.SetImage (url,null,SDWebImageOptions.None,null,(img,error,cache)=>{this.LoadingView.StopAnimating();});//this.webImageView.SetImage (url,UIImage.FromFile("temp.png"),SDWebImageOptions.None,null,null);}

   2.SDWebImageManager单例

   1)图片下载

private void SDLoad(){string url = "http://g.hiphotos.baidu.com/image/pic/item/3bf33a87e950352a808d060f5043fbf2b3118bcc.jpg";var manager = SDWebImageManager.SharedManager;manager.Download (url,SDWebImageOptions.None,null,CompletedHandler);}void CompletedHandler (UIImage image, NSError error, SDImageCacheType cacheType,bool finshed){Console.WriteLine ("Reuslt>>"+finshed);this.InvokeOnMainThread (()=>{this.TableView.BackgroundColor = UIColor.FromPatternImage(image);});}

   2)内存清理及本地删除

private void DelClcik( ){SDWebImageManager.SharedManager.ImageCache.ClearMemory ();//清空内存SDWebImageManager.SharedManager.ImageCache.ClearDisk ();//清除本地}

   SDWebImage与进度条使用

   1.RadialProgressView/UIProgressView

   三种类型Big,Small,Tiny

public override void ViewDidLoad (){base.ViewDidLoad ();// Add our different styles of RadialProgressViewsbigRadialProgressView = new RadialProgressView ();bigRadialProgressView.Center = new PointF (View.Center.X, View.Center.Y - 100);bigRadialProgressView.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;View.AddSubview (bigRadialProgressView);smallRadialProgressView = new RadialProgressView (RadialProgressViewStyle.Small);smallRadialProgressView.ProgressColor = UIColor.Gray;smallRadialProgressView.Center = new PointF (bigRadialProgressView.Frame.Left / 2, bigRadialProgressView.Center.Y);smallRadialProgressView.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;View.AddSubview (smallRadialProgressView);tinyRadialProgressView = new RadialProgressView (RadialProgressViewStyle.Tiny);tinyRadialProgressView.ProgressColor = UIColor.White;tinyRadialProgressView.Center = new PointF (bigRadialProgressView.Frame.Right + (View.Frame.Width - bigRadialProgressView.Frame.Right) / 2, bigRadialProgressView.Center.Y);tinyRadialProgressView.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;View.AddSubview (tinyRadialProgressView);standardProgressView = new UIProgressView (UIProgressViewStyle.Bar);standardProgressView.Frame = new RectangleF (30, bigRadialProgressView.Frame.Bottom + 80, View.Frame.Width - 60, 10);standardProgressView.AutoresizingMask = UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleWidth;View.AddSubview (standardProgressView);startProgressButton = UIButton.FromType (UIButtonType.RoundedRect);startProgressButton.Frame = new RectangleF (50, standardProgressView.Frame.Bottom + 40, View.Frame.Width - 100, 30);startProgressButton.SetTitle ("Start Progress", UIControlState.Normal);startProgressButton.AutoresizingMask = UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleWidth;startProgressButton.TouchUpInside += OnStartProgressTapped;View.AddSubview (startProgressButton);}

   2.SDWebImage回调操作

void OnStartProgressTapped (object sender, EventArgs e){string url = "http://g.hiphotos.baidu.com/image/pic/item/3bf33a87e950352a808d060f5043fbf2b3118bcc.jpg";standardProgressView.Progress = 0;bigRadialProgressView.Reset ();smallRadialProgressView.Reset ();tinyRadialProgressView.Reset ();this.ImageView.SetImage (url,null, SDWebImageOptions.SDWebImageProgressiveDownload, ProgressHandler, CompletedHandler);}/// <summary>/// 实时下载数据/// </summary>/// <param name="receivedSize">当前下载量.</param>/// <param name="expectedSize">总量.</param>void ProgressHandler (uint receivedSize, long expectedSize){Console.WriteLine ("receivedSize -- expectedSize -- "+receivedSize+" -- " + expectedSize);var value = (float)receivedSize / (float)expectedSize;if(value != 0){Console.WriteLine ("Value>>"+value);this.InvokeOnMainThread (()=>{bigRadialProgressView.Value = value;smallRadialProgressView.Value = value;tinyRadialProgressView.Value = value;standardProgressView.Progress = value;});}}void CompletedHandler (UIImage image, NSError error, SDImageCacheType cacheType){this.InvokeOnMainThread (()=>{UIView.Animate (0.1f,()=>{bigRadialProgressView.Alpha = 0.0f;smallRadialProgressView.Alpha = 0.0f;tinyRadialProgressView.Alpha = 0.0f;standardProgressView.Alpha = 0.0f;},()=>{bigRadialProgressView.RemoveFromSuperview();smallRadialProgressView.RemoveFromSuperview();tinyRadialProgressView.RemoveFromSuperview();standardProgressView.RemoveFromSuperview();});});}

   效果


参考资源

源码:Xamarin studio(5.5.2) Xamarin.iOS(8.2.0.207) Xcode(5.1.1)

原生(SDWebImage)>>https://github.com/rs/SDWebImage/

Xamarin组件(SDWebImage)>>http://components.xamarin.com/view/sdwebimage

Xamarin组件Binding(SDWebImage)>>https://github.com/stampsy/sdwebimage-monotouch

Xamarin组件圆形进度条(Radialprogress)>>http://components.xamarin.com/view/radialprogress

Xamarin三方Binding>>https://github.com/mono/monotouch-bindings

0 0
原创粉丝点击