UIGraphics(using CoreGraphics;)是iOS里面经常用到的绘图类,下面就简单的使用这个类来完成指定颜色及大小生成图片的操作:
Classic API源码
private UIImage ImageFromColor(UIColor color,SizeF size)
{
UIGraphics.BeginImageContext (size);
var context = UIGraphics.GetCurrentContext ();
context.SetFillColorWithColor (color.CGColor);
context.FillRect (new RectangleF(new PointF(0,0),size));
UIImage img = UIGraphics.GetImageFromCurrentImageContext ();
UIGraphics.EndImageContext ();
return img;
}
Unified API源码
private UIImage ImageFromColor(UIColor color,CGSize size)
{
UIGraphics.BeginImageContext (size);
var context = UIGraphics.GetCurrentContext ();
context.SetFillColor (color.CGColor);
context.FillRect (new CGRect(new CGPoint(0,0),size));
UIImage img = UIGraphics.GetImageFromCurrentImageContext ();
UIGraphics.EndImageContext ();
return img;
}
Classic API与Unified API之间有什么不同:
Classic是Xamarin早期使用的API,只支持32位的应用开发。后来苹果新规之后,Xamarin为了与苹果接轨,在Classic的基础上升级一套Unified,其中使用的方法类名更接近于苹果源生API,并且同时兼容64位系统。所以如果是要上Appstore的应用在新规下,必须使用Unified API。