私はアプリに取り組んでおり、ある時点でユーザーが画像の上に何かを描く必要があります。私が書いたコードは、周りの画像1500x1500
や小さい画像では問題なく機能しますが、画像が大きくなると問題が始まります。
画像が大きくなりすぎると、描画に時間がかかり、Gesture Recognizer がほとんど呼び出されなくなります。
2 つのクラスがあり、1 つは というUIScrollView
サブクラスで、DrawView
もう 1 つは というUIImageView
サブクラスMyPen
です。認識されるたびにメッセージを送信する がDrawView
あります ( を取得し、それに応じて行を開始または移動します)。サブビューに 2 つの UIImageView オブジェクトがあり、1 つは背景画像用、もう 1 つは描画 (ペン) 用です。UIPanGestureRecognizer
MyPen
[recognizer state]
DrawView
MyPen で行うことは次のとおりです。
- (void)beginLine:(CGPoint) currentPoint
{
previousPoint = currentPoint;
}
- (void)moveLine:(CGPoint) currentPoint
{
self.image = [self drawLineFromPoint:previousPoint toPoint:currentPoint image:self.image];
previousPoint = currentPoint;
}
- (UIImage *)drawLineFromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint image:(UIImage *)image
{
CGSize screenSize = self.frame.size;
UIGraphicsBeginImageContext(screenSize);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
[image drawInRect:CGRectMake(0, 0, screenSize.width, screenSize.height)];
CGContextSetLineCap(currentContext, kCGLineCapRound);
CGContextSetLineWidth(currentContext, _thickness);
CGContextSetStrokeColorWithColor(currentContext, _color);
CGContextBeginPath(currentContext);
CGContextMoveToPoint(currentContext, fromPoint.x, fromPoint.y);
CGContextAddLineToPoint(currentContext, toPoint.x, toPoint.y);
CGContextStrokePath(currentContext);
UIImage *ret = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return ret;
}
画像が十分に大きい場合、CoreGraphics がレンダリングするのに時間がかかるため、ジェスチャ レコグナイザーが認識される頻度が低くなり、ペンに送信されるポイントが少なくなります。
ここでの質問は次のとおりです。図面を最適化する方法はありますか? ir に別のスレッドを使用する必要がありますか? それを回避する別の方法はありますか?どんな助けでも大歓迎です。