フレームワークを介してブラシのような効果を得るために画像をストロークして線を引こうとしていCoreGraphics
ます。
この投稿とこの投稿をフォローして、メソッドから描画しようとしましたがtouchesMoved
、コードサンプルは次の形式になります
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
UIImage *texture = [UIImage imageNamed:@"texture.png"];
CGPoint vector = CGPointMake(currentPoint.x - lastPoint.x, currentPoint.y - lastPoint.y);
NSLog(@" vector %@", NSStringFromCGPoint(vector));
CGFloat distance = hypotf(vector.x, vector.y);
vector.x /= distance;
vector.y /= distance;
for (CGFloat i = 0; i < distance; i += 1.0f) {
CGPoint p = CGPointMake(lastPoint.x + i * vector.x, lastPoint.y + i * vector.y);
[texture drawAtPoint:p blendMode:kCGBlendModeNormal alpha:1.0f];
NSLog(@"p %@", NSStringFromCGPoint(p));
}
previousPoint = currentPoint;
そうすることで、エラーメッセージが表示されました。
Jul 9 11:51:27 mac1.local Smooth Line View[1035] <Error>: CGContextSaveGState: invalid context 0x0
Jul 9 11:51:27 mac1.local Smooth Line View[1035] <Error>: CGContextSetBlendMode: invalid context 0x0
Jul 9 11:51:27 mac1.local Smooth Line View[1035] <Error>: CGContextSetAlpha: invalid context 0x0
Jul 9 11:51:27 mac1.local Smooth Line View[1035] <Error>: CGContextTranslateCTM: invalid context 0x0
Jul 9 11:51:27 mac1.local Smooth Line View[1035] <Error>: CGContextScaleCTM: invalid context 0x0
Jul 9 11:51:27 mac1.local Smooth Line View[1035] <Error>: CGContextDrawImage: invalid context 0x0
Jul 9 11:51:27 mac1.local Smooth Line View[1035] <Error>: CGContextRestoreGState: invalid context 0x0
それで、コンテキストに関連するコードを配置し、変更されたコードは
UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:self];
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextSetAlpha(context, 1.0f);
CGContextSaveGState(context);
UIImage *texture = [UIImage imageNamed:@"textureImg.png"];
[texture drawAtPoint:currentPoint blendMode:kCGBlendModeNormal alpha:1.0f];
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, 25.0);
CGContextSetRGBStrokeColor(context, texture.CGColor);
CGContextBeginPath(context);
CGContextMoveToPoint(context, previousPoint.x, previousPoint.y);
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
CGContextDrawPath(context, kCGPathFillStroke);
UIGraphicsEndImageContext();
しかし、走ってタッチムーブで描画しようとすると、何も描画されません。何が悪かったのかわかりません。誰かが私の間違いを指摘できますか?私はまだこれを整理するのに多くの時間を費やしていますが、今はまだできません。タイムリーなヘルプをいただければ幸いです。ありがとう。