Draw line on image by handgetsture in objective c

2019-08-31 16:21发布

I have set image's content mode aspect fit. Now issue is that, when I'm drawing line on image, that time image's content mode set scaletofill and my image stretched. So I want solution for when I'm drawing line on image, image content mode remain same.

U can download myproject from this link.

I'm using following code:

- (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];

}

In this code, view frame set in UIGraphicsBeginImageContext. I think it will possible using subclass of UIImageView. I don't know exact solution.

Please see this Screenshot, u will easily understand my problem enter image description here Thanks in advance

0条回答
登录 后发表回答