gpt4 book ai didi

ios - 在 iOS 中捕获签名时出现性能问题

转载 作者:可可西里 更新时间:2023-11-01 05:29:58 24 4
gpt4 key购买 nike

我有一个签名控件,效果很好,除非你的名字很长,然后它开始显示空白。似乎与性能有关,但在模拟器和最新的 iPad 上都是一样的。我在下面附上了一个示例项目以及签名绘图代码。

如有任何帮助,我们将不胜感激!

https://dl.dropboxusercontent.com/u/25670071/SignatureArchive.zip

enter image description here

 using System;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;
using System.Drawing;

namespace MyApp
{
public class SignatureViewV2 : UIView
{
public delegate void SignatureChanged();
public SignatureChanged OnSignatureChanged;

private bool _empty = true;
// clear the canvas
public void Clear ()
{
drawPath.Dispose ();
drawPath = new CGPath ();
fingerDraw = false;
SetNeedsDisplay ();
_empty = true;
}

public bool IsEmpty ()
{
return _empty;
}

public SignatureViewV2 (RectangleF frame) : base(frame)
{

this.drawPath = new CGPath ();
this.BackgroundColor = UIColor.White;

}


private PointF touchLocation;
private PointF prevTouchLocation;
private CGPath drawPath;
private bool fingerDraw;

public override void TouchesBegan (MonoTouch.Foundation.NSSet touches, UIEvent evt)
{
base.TouchesBegan (touches, evt);

UITouch touch = touches.AnyObject as UITouch;
this.fingerDraw = true;
this.touchLocation = touch.LocationInView (this);
this.prevTouchLocation = touch.PreviousLocationInView (this);
this.SetNeedsDisplay ();

}

public override void Draw (RectangleF rect)
{
base.Draw (rect);

if (this.fingerDraw) {
using (CGContext context = UIGraphics.GetCurrentContext()) {
context.SetStrokeColor (UIColor.FromRGB(63, 112, 185).CGColor);
context.SetLineWidth (2f);
context.SetLineJoin (CGLineJoin.Round);
context.SetLineCap (CGLineCap.Round);
this.drawPath.MoveToPoint (this.prevTouchLocation);
this.drawPath.AddLineToPoint (this.touchLocation);
context.AddPath (this.drawPath);
context.DrawPath (CGPathDrawingMode.Stroke);
}
if(OnSignatureChanged != null)
OnSignatureChanged();
_empty = false;
}
}

public override void TouchesMoved (MonoTouch.Foundation.NSSet touches, UIEvent evt)
{
base.TouchesMoved (touches, evt);

UITouch touch = touches.AnyObject as UITouch;
this.touchLocation = touch.LocationInView (this);
this.prevTouchLocation = touch.PreviousLocationInView (this);
this.SetNeedsDisplay ();
}

public UIImage GetDrawingImage ()
{
UIImage returnImg = null;

UIGraphics.BeginImageContext (this.Bounds.Size);

using (CGContext context = UIGraphics.GetCurrentContext()) {
context.SetStrokeColor (UIColor.FromRGB(63, 112, 185).CGColor);
context.SetLineWidth (5f);
context.SetLineJoin (CGLineJoin.Round);
context.SetLineCap (CGLineCap.Round);
context.AddPath (this.drawPath);
context.DrawPath (CGPathDrawingMode.Stroke);
returnImg = UIGraphics.GetImageFromCurrentImageContext ();
}

UIGraphics.EndImageContext ();

return returnImg;
}



}

最佳答案

你不能依赖 Draw()被称为每个TouchesMoved() .如果Draw()每 2 次触摸被调用一次,你会得到所描述的间隙。

我会通过排队(例如在 Queue<T> 中)TouchesMoved() 中的触摸来解决这个问题并在 Draw() 中出队

您可能还有另一个问题:在每个 Draw() ,您每次都将完整路径重新添加到当前路径。您可能只需调用 AddPath 即可解决此问题对于新的分割市场或调用 AddPath()一次,将段添加到您的路径(`Move,AddLine)并重新绘制它。但我还没有测试过这些。

关于ios - 在 iOS 中捕获签名时出现性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18979843/

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