通过物镜在目标c中在图像上画线

时间:2018-07-27 07:01:49

标签: ios objective-c uiimageview drawing

我已经设置了图像的内容模式外观。 现在的问题是,当我在图像上画线时,该图像的内容模式设置了scaletofill并拉伸了我的图像。因此,当我在图像上画线时,我想要的解决方案是图像内容模式保持不变。

您可以从此link下载myproject。

我正在使用以下代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(self.tempImage.image==nil)
    {
        return;
    }
    colorPicker.hidden = YES;
    UITouch *touch = [touches anyObject];
    lastPoint = [touch locationInView:self.view];
    lastPoint.y -= 20;
    _pointsArray = [NSMutableArray array];
    [_pointsArray addObject:NSStringFromCGPoint(lastPoint)];

    UIGraphicsBeginImageContext(self.view.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.tempImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, brush);
    CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), self.selectedColor.CGColor);
    CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(context, lastPoint.x, lastPoint.y);
    CGContextStrokePath(context);
    self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();

    [self setupExternalScreen];


    UIGraphicsEndImageContext();

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    if(self.tempImage.image==nil)
    {
        return;
    }
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];
    currentPoint.y -= 20;
    UIGraphicsBeginImageContext(self.view.frame.size);
    [self.tempImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);

    //Now set our brush size and opacity and brush stroke color:
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, brush);
    CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), self.selectedColor.CGColor);
    CGContextSetBlendMode(context,kCGBlendModeNormal);
    CGContextStrokePath(context);

    self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();
    [self.tempImage setAlpha:opacity];
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;

    [self setupExternalScreen];
    [self.pointsArray addObject:NSStringFromCGPoint(lastPoint)];

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if(self.tempImage.image==nil)
    {
        return;
    }
    //handle single tap, make _pointsArray has two identical points, draw a line between them
    if (_pointsArray.count == 1) {
        [_pointsArray addObject:NSStringFromCGPoint(lastPoint)];
    }

    [self.stack addObject:_pointsArray];
    NSLog(@"color -> %@ \nwidth->%f", self.selectedColor.description, brush);

    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:self.selectedColor forKey:@"color"];
    [dic setObject:[NSNumber numberWithFloat:brush] forKey:@"width"];
    [self.contextArray addObject:dic];


    [self setupExternalScreen];
    [self.undoManager registerUndoWithTarget: self
                                    selector: @selector(popDrawing)
                                      object: nil];

}

在此代码中,查看在UIGraphicsBeginImageContext中设置的视图框架。我认为可以使用UIImageView的子类。我不知道确切的解决方案。

请查看此屏幕截图,您将轻松理解我的问题 enter image description here 预先感谢

1 个答案:

答案 0 :(得分:0)

在我的旧项目中,我需要通过该应用获取用户签名,在这种情况下,我使用“ PPSSignatureView”窗格。我认为它将为您提供帮助。请参阅此链接。

https://github.com/jharwig/PPSSignatureView

感谢。