ฉันกำลังสร้างหมุดแผนที่แบบกำหนดเอง (คำอธิบายประกอบ) สำหรับ iOS วาดเส้นทางของมันแล้วแปลง UIView
เป็นรูปภาพ โดยใช้ วิธีการ:
public static UIImage AsImage(this UIView view)
{
try
{
UIGraphics.BeginImageContextWithOptions(view.Bounds.Size, true, 0);
var ctx = UIGraphics.GetCurrentContext();
view.Layer.RenderInContext(ctx);
var img = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return img;
}
catch (Exception)
{
return null;
}
}
และนี่คือคลาสที่ได้รับ UIView
ซึ่งฉันวาดเส้นทางคำอธิบายประกอบ:
public class PinView : UIView
{
public PinView()
{
ContentMode = UIViewContentMode.Redraw;
SetNeedsDisplay();
}
private readonly CGPoint[] markerPathPoints = new[]
{
new CGPoint(25f, 5f),
new CGPoint(125f, 5f),
//other points };
public override void Draw(CGRect rect)
{
base.Draw(rect);
//get graphics context
CGContext context = UIGraphics.GetCurrentContext();
//set up drawing attributes
context.SetLineWidth(10);
UIColor.White.SetFill();
UIColor.Black.SetStroke();
//create geometry
var path = new CGPath();
path.MoveToPoint(markerPathPoints[0].X, markerPathPoints[0].Y);
for (var i = 1; i < markerPathPoints.Length; i++)
{
path.AddLineToPoint(markerPathPoints[i].X, markerPathPoints[i].Y);
}
path.CloseSubpath();
//add geometry to graphics context and draw it
context.AddPath(path);
context.DrawPath(CGPathDrawingMode.FillStroke);
}
ฉันกำลังแก้ไข Xamarin.Forms ตัวอย่าง MapCustomRenderer แทนที่จะโหลดคำอธิบายประกอบจากไฟล์ ฉันกำลังโหลดจากมุมมอง:
annotationView.Image = new PinView().AsImage();
แต่ PinView.Draw()
ไม่เคยถูกเรียก!