0

ここに画像の説明を入力してください

描画領域のサイズを小さくしようとしていますが、サイズを変更すると、この種の出力が表示されます(写真を参照)。

私が書き始めるとき、それは線を引かず、ただすべてを伸ばすだけです。

私は何が問題なのかを理解しようとしていますが、私が知っていることは十分ではありません。

これが私のコードです:

viewDidLoad:

touchDraw = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"opaque.png"]];
    touchDraw.frame = CGRectMake(0, 0, 768, 1024);
    [self.view addSubview:touchDraw];
    [self.view sendSubviewToBack:touchDraw];
    touchMoved = 0;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    touchSwiped = NO;
    UITouch *touch = [touches anyObject];

    endingPoint = [touch locationInView:self.view];
    endingPoint.y -= 35;

}

タッチ:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    touchSwiped = YES;
    UITouch *touch = [touches anyObject];   
    currentTouch = [touch locationInView:self.view];
    currentTouch.y -= 35;
    UIGraphicsBeginImageContext(self.view.frame.size);
    [touchDraw.image drawInRect:CGRectMake(0, 0, 768, 1024)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    UIImage * textureColor = [UIImage imageNamed:@"Brush_Black.png"];

    CGPoint vector = CGPointMake(currentTouch.x - endingPoint.x, currentTouch.y - endingPoint.y);
    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(endingPoint.x + i * vector.x, endingPoint.y + i * vector.y);
        [textureColor drawAtPoint:p blendMode:blendMode alpha:0.5f];
    }
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    touchDraw.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    endingPoint = currentTouch;

    touchMoved++;

    if (touchMoved == 1) {
        touchMoved = 0;
    }

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(!touchSwiped) {
        UIGraphicsBeginImageContext(self.view.frame.size);
        [touchDraw.image drawInRect:CGRectMake(0, 0, touchDraw.frame.size.width, touchDraw.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 25.0);
        UIImage * textureColor = [UIImage imageNamed:@"Brush_Black.png"];
        [textureColor drawAtPoint:CGPointMake(endingPoint.x, endingPoint.y) blendMode:blendMode alpha:1.0f];
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        touchDraw.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }

}
4

1 に答える 1

1

この行を変更

UIGraphicsBeginImageContext(self.view.frame.size);

このように

UIGraphicsBeginImageContext(touchDraw.frame.size);

これが役立つ場合があります。

于 2012-02-16T06:58:39.020 に答える