45782

Draw line on image by handgetsture in objective c

Question:

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 <a href="https://drive.google.com/file/d/1UxrjBQBb-LyrORze9q6qPRIPcTFc-PAd/view?usp=sharing" rel="nofollow">link</a>.

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 <a href="https://i.stack.imgur.com/Ho9qf.jpg" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/Ho9qf.jpg" data-original="https://i.stack.imgur.com/Ho9qf.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a> Thanks in advance

Answer1:

In my old project i need to get the users signature through the app, in that situation i use the pod "PPSSignatureView". I think it will help you. Refer this link.

<a href="https://github.com/jharwig/PPSSignatureView" rel="nofollow">https://github.com/jharwig/PPSSignatureView</a>

Thanx.

Recommend

  • IOS - Centering UIScrollView inside UIView container programmatically with Autolayout issue
  • Add class to even elements when even element is clicked and add class to odd elements when odd eleme
  • Generate a CGPath from another path's line width outline
  • CATiledLayer or CALayer drawing UIImage not working
  • SpriteKit childNodeWithName can't find existing node
  • drawRect called after creating a custom UI?
  • Simple drawing with Quartz - Core Graphics
  • How can I show an image in a NSView using an CGImageRef image
  • How to sort plist by its value
  • How to prevent the pixels from becoming blurry when I enlarge an image in my color pick project for
  • Extra line when drawing an arc in swift
  • Trimming UIImage white borders
  • How to cut out a hole with radial gradient in view
  • iOS rotate UIImage using Quartz
  • Link conversion in mod rewrite .htaccess
  • how to draw vertical line in UIScrollView
  • How to draw full UIImage inside a square with white color on the edge
  • Why is my image upside down after using CGContextSetFillColorWithColor
  • Take Screenshot of a UIImage iOS
  • Set UITableViewCell background color using a CALayer
  • Why is my image upside down after using CGContextSetFillColorWithColor
  • iOS Screen Capture is blurry
  • Unable to save NSMutableArray of my class to file (IOS)
  • Archive validation error NSSetM
  • QTP OR Automation Model : Adding Multiple objects (Same type) to QTP Object Repository
  • Save disconnected object in entity framework 4
  • UItableviewcell background color while click
  • Delphi TCollection - Possible to prevent changing items in run-time?
  • unrecognized selector with category NSMutableData
  • Row Count Is Returning the incorrect number using RaptureXML
  • Initializer list vs. initialization method
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Date difference with leap year
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • IndexOutOfRangeException on multidimensional array despite using GetLength check