gpt4 book ai didi

xamarin - 绘制成 UIImage

转载 作者:行者123 更新时间:2023-12-04 17:27:46 24 4
gpt4 key购买 nike

如何使用单点触控绘制到现有的 UIImage 中?

我加载一个图像: UIImage.FromFile("MyImage.png")

然后我想在这个图像中绘制一个字符串和一些线条。

有人有代码示例吗?

谢谢

最佳答案

这是一个方法:

private void drawOnTouch(object data)
{

UITouch touch = data as UITouch;

if (null != touch)
{

UIGraphics.BeginImageContext(this.Image == null ? this.Frame.Size : this.Image.Size);

using (CGContext cont = UIGraphics.GetCurrentContext())
{

if (this.Image != null)
{

cont.TranslateCTM(0f, this.Image.Size.Height);
cont.ScaleCTM(1.0f, -1.0f);
cont.DrawImage(new RectangleF(0f,0f,this.Image.Size.Width, this.Image.Size.Height), this.Image.CGImage);
cont.ScaleCTM(1.0f, -1.0f);
cont.TranslateCTM(0f, -this.Image.Size.Height);


} //end if

PointF lastLocation = touch.PreviousLocationInView(this);
PointF pt = touch.LocationInView(this);
using (CGPath path = new CGPath())
{

cont.SetLineCap(CGLineCap.Round);
cont.SetLineWidth(3);
cont.SetRGBStrokeColor(0, 2, 3, 1);
path.MoveToPoint(lastLocation.X, lastLocation.Y);
path.AddLines(new PointF[] { new PointF(lastLocation.X,
lastLocation.Y),
new PointF(pt.X, pt.Y) });
path.CloseSubpath();

cont.AddPath(path);
cont.DrawPath(CGPathDrawingMode.FillStroke);
this.Image = UIGraphics.GetImageFromCurrentImageContext();

}//end using path


}//end using cont
UIGraphics.EndImageContext();
this.SetNeedsDisplay();

}//end if


}//end void drawOnTouch

如果将此方法放在 UIImageView 的子类中并从 TouchesBegan 和 TouchesMoved 调用它,则当您触摸屏幕时,它将在图像上绘制。

关于xamarin - 绘制成 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5461819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com