Окраска / нанесение кода. Как сделать его более гладким с меньшей непрозрачностью? Удалить точки кисти? - PullRequest
0 голосов
/ 02 мая 2018

У меня есть приложение для iOS с функцией типа книжки-раскраски. Код, который я использую, работает для раскраски. При полной непрозрачности качество линии выглядит хорошо, но при более низкой непрозрачности вы можете видеть отдельные точки кисти.

Код:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    pointCurrent = [touch locationInView:self.view];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touch = [touches anyObject];
    CGPoint pointNext = [touch locationInView:self.view];
    UIGraphicsBeginImageContext(img.frame.size);
    //    UIGraphicsBeginImageContext(img2.frame.size);
    [img.image drawInRect:CGRectMake(0, 0, img.frame.size.width, img.frame.size.height)];
    //  [img2.image drawInRect:CGRectMake(0, 0, img2.frame.size.width, img2.frame.size.height)];
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
    /// CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), a, b, c, d);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), pointCurrent.x, pointCurrent.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), pointNext.x, pointNext.y);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(),kCGLineCapRound);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    img.image = UIGraphicsGetImageFromCurrentImageContext();
    // img2.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    pointCurrent = pointNext;
}

результат: enter image description here

1 Ответ

0 голосов
/ 02 мая 2018

Проблема в том, что вы обновляете изображение, рисуя только последний отрезок линии поверх изображения. Поскольку цвет линии частично прозрачен, вы видите наложение каждого сегмента.

Лучшим вариантом было бы создать UIBezierPath и продолжать добавлять к нему каждый новый сегмент. Затем создайте новое изображение каждый раз из безье пути. Это устранит перекрытия сегментов.

Примерно так (не проверено, но вам следует начать):

Добавить переменную экземпляра:

UIBezierPath *_path;

Затем обновите ваш код:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint pointCurrent = [touch locationInView:self.view];
    _path = [[UIBezierPath alloc] init];
    _path.lineWidth = ... // set the width
    _path.lineCapStyle = ... // set the style
    _path.lineJoinStyle = ... // set the style
    [_path moveToPoint:pointCurrent];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint pointNext = [touch locationInView:self.view];
    [_path addLineToPoint:pointNext];

    UIGraphicsBeginImageContext(img.frame.size);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), a, b, c, d);

    [_path stroke];

    img.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}
...