i'm using the following code to draw lines over an imageview's image,
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2)
return;
lastPoint = [touch locationInView:imageview];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), 3);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5);
UIGraphicsBeginImageContext(self.view.frame.size);
[imageview.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextClearRect(UIGraphicsGetCurrentContext(), CGRectMake(lastPoint.x, lastPoint.y, 5, 5));
imageview.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
Output
You can see the gaps/spaces between the dots. I need the output should be like below,
What should I change in my code to remove the space between two dots?
I think, touchesended code is not needed for your review. I changed the CGContextSetLineCap and width, no luck.
I would change the approach, essentially you're capturing touch points, and not "joining the dots" so to speak. You would be better off using UIBezierPaths for this task.
I answered a question some time back where the op wanted to do something similar but was hitting performance issues. There is code in the answer i posted that pretty much does what you want, completely. Review this post for full sample code, you can pretty much cut and paste the class (I see little value in transcribing). Sample drawing path
If you need more help just let me know
Related
I want to annotate photos for an iOS App, i was searching if any library exist that would help me set the base.
Basically i am looking to implement similar functionality like skitch app : https://itunes.apple.com/us/app/skitch-snap.-mark-up.-send./id490505997?mt=8
The feature i am working on requires the user of application to capture a picture, highlight specific area of the picture and comment on it.
Any leads on how to go about it is appreciated.
Thanks.
Marco Arment, the creator of BugShot, open sourced BugShotKit to let developers add support for annotation easily. You can have a look at the project here https://github.com/marcoarment/BugshotKit
Try this , i hope this will help you
#pragma mark touches stuff...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
lastTouch = [touch locationInView:imageView]; // This is your view
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
currentTouch = [touch locationInView:imageView];
CGColorRef strokeColor = [UIColor brownColor].CGColor;
UIGraphicsBeginImageContext(imageView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context,10);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor brownColor].CGColor);
CGContextSetBlendMode(context,kCGBlendModeDarken );
CGContextBeginPath(context);
CGContextMoveToPoint(context, lastTouch.x, lastTouch.y);
CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
CGContextStrokePath(context);
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastTouch = [touch locationInView:imageView];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
lastTouch = [touch locationInView:imageView];
}
I have looked through all the packages used by SkitchSnap and these is the basic libraries you should take a look at:
https://github.com/mattgemmell/MGImageUtilities
https://github.com/coryalder/UIImage_Resize
They also have a lot of other libraries that you should look in to.
Example: To get the gray effect, user-login, converting SVG objects, testing..
Basically I just want to make an eraser just like in Paint. I want to erase parts of a UIImageView just with my finger. I use the following code, however, it erases only with small strokes, while I want it to erase until I pull my finger off:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
lastTouch = [touch locationInView:bg];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGFloat brushSize = 35;
CGColorRef strokeColor = [UIColor clearColor].CGColor;
UITouch *touch = [touches anyObject];
currentTouch = [touch locationInView:bg];
UIGraphicsBeginImageContext(bg.frame.size);
CGContextRef context2 = UIGraphicsGetCurrentContext();
[bg.image drawInRect:CGRectMake(0, 0, bg.frame.size.width, bg.frame.size.height)];
CGContextSetLineCap(context2, kCGLineCapRound);
CGContextSetLineWidth(context2, brushSize);
CGContextSetStrokeColorWithColor(context2, strokeColor);
CGContextSetBlendMode(context2, kCGBlendModeClear);
CGContextBeginPath(context2);
CGContextMoveToPoint(context2, lastTouch.x, lastTouch.y);
CGContextAddLineToPoint(context2, currentTouch.x, currentTouch.y);
CGContextStrokePath(context2);
bg.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastTouch = currentTouch;
}
How should I change my code?
hi i have done getting image from Gallery or camera.and i have button when i click that button getting image again from gallery or camera in which when i touch some part that is highlighting that should be only display in to anotherView.remaing part should be removed.please anybody assist me how to do this.help is appreciated. i written code this
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.slider.hidden=NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view]; //lastTouch is CGPoint declared globally
}
//#pragma mark - Touch moved mehotd
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:self.view1]; //currentTouch is CGPoint declared globally
CGColorRef strokeColor = [UIColor whiteColor].CGColor;
UIGraphicsBeginImageContext(view1.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[imageView1.image drawInRect:CGRectMake(0, 0, self.view1.frame.size.width, self.view1.frame.size.height)];
//canvasView is your UIImageView object declared globally
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, slider.value);
CGContextSetStrokeColorWithColor(context, strokeColor);
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextBeginPath(context);
CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
CGContextStrokePath(context);
imageView1.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = [touch locationInView:self.view1];
}
//#pragma mark - Touch end mehotd
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:self.view1];
}
but it is erasing selected Area.but i don't want to erase the selected Area(highlight).unselected part should be erased .i tried a lot please any body reply me how to do this.help is appreciated
your question sounds good. I think somebody already give answer earlier like same query.
Please have a look following
Let User Crop Photo in iOS App
i hope will help you. All the best!! :)
i'm researching on the time it will take and how to make a drawing pad for a elementary school kids. So it will be similar draw something, but only with the draw pad. Kids will be able to change different colors to draw it. So which frameworks should I look at? core graphics? Are there anything else? Can anyone point to a tutorial or directions? Thanks in advance.
Try this code ....
#pragma mark touches stuff...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
lastTouch = [touch locationInView:imageView];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
currentTouch = [touch locationInView:imageView];
CGColorRef strokeColor = [UIColor brownColor].CGColor;
UIGraphicsBeginImageContext(imageView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context,10); // 10 is the pen size
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor brownColor].CGColor); // this will be colour u need to change as required
CGContextSetBlendMode(context,kCGBlendModeDarken );
CGContextBeginPath(context);
CGContextMoveToPoint(context, lastTouch.x, lastTouch.y);
CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
CGContextStrokePath(context);
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastTouch = [touch locationInView:imageView];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
lastTouch = [touch locationInView:imageView];
}
I hope this will help you ....
I'm studying Quartz now and want to do a demo like this:
when your finger moves on the iPhone screen, it shows the track in red color。
The code is like:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
_firstPoint = [touch locationInView:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
_endPoint = [touch locationInView:self];
[self setNeedsDisplay];
_firstPoint = _endPoint;
}
then
- (void)drawRect:(CGRect)rect {
// Drawing code.
CGContextRef _context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(_context, 1, 0, 0, 1);
CGContextMoveToPoint(_context, _firstPoint.x, _firstPoint.y);
CGContextAddLineToPoint(_context, _endPoint.x, _endPoint.y);
CGContextStrokePath(_context);
}
Here,_firstPoint and _endPoint are CGPoint to record positions.
However, it doesn't show the track.
I don't know what is the problem.
Please give any tips.
Finally, I'd like to consultant whether it is right to fulfill such a kind of App.
thanks!
To your point about where the collection of points making up the lines is stored, it is not stored in this example.
EDITED
Yeah, to store them, I'd just add to a NSMutableArray.
something like
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!_points) _points = [[NSMutableArray array] retain];
UITouch *touch = [touches anyObject];
[_points addObject:[NSValue valueWithCGPoint:[touch locationInView:self]];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
[_points addObject:[NSValue valueWithCGPoint:[touch locationInView:self]];
[self setNeedsDisplay];
}
The setNeedsDisplay is going to invoke the drawRect that's where you use the points and your draw methods.
you can read this tutorial to figured it out - may be it helps
http://www.ifans.com/forums/showthread.php?t=132024
i think you missed CGContextBeginPath(...) first of all
good luck!