Objective C 调整绘图区域的大小

发布于 2025-01-06 20:40:54 字数 2779 浏览 2 评论 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();
    }

}

enter image description here

I am trying to resize my drawing area to a smaller size but whenever i do that i'm having this kind of output (see the photo).

When i start to write it doesn't draw a line and just stretches everything.

i trying to figure out what's the problem but what i know is not enough.

Here's my code:

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;

}

Touches:

- (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();
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑疤怪 2025-01-13 20:40:54

将此行更改

UIGraphicsBeginImageContext(self.view.frame.size);

为这

UIGraphicsBeginImageContext(touchDraw.frame.size);

可能会有所帮助。

Change this line

UIGraphicsBeginImageContext(self.view.frame.size);

as this

UIGraphicsBeginImageContext(touchDraw.frame.size);

This may help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文