How to erase part of UIImageView with finger? - ios

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?

Related

Annotate photos using objective-c

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..

how to remove the part of image except highlighting part in ios

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!! :)

Crop image when draw line with hand

I have a Image view added on a view. View is using UIBezierPath and touch responder methods for drawing line upon dragging on screen. Now I would like to clip the part of image that are below the line. How can I achieve this.
I use below methods to draw line
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path moveToPoint:p];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path addLineToPoint:p]; // (4)
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesMoved:touches withEvent:event];
}
Try this:
#property (strong, nonatomic) UIBezierPath *bezierPath;
//...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
//...
self.bezierPath = [UIBezierPath bezierPath];
[self.bezierPath moveToPoint:p];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
//...
[self.bezierPath addLineToPoint:p];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesMoved:touches withEvent:event];
// Here you can call -croppedImage; method and get the image you need.
}
- (UIImage *)croppedImage
{
[self.bezierPath closePath];
UIImage *image = //Your initial image;
CGSize imageSize = image.size;
CGRect imageRect = CGRectMake(0, 0, imageSize.width, imageSize.height);
UIGraphicsBeginImageContextWithOptions(imageSize, NO, [[UIScreen mainScreen] scale]);
// Create the clipping path and add it
[self.bezierPath addClip];
[image drawInRect:imageRect];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return croppedImage;
}
This is a fast written code, so can be possible mistakes. Sorry for that :)

How to allow users to draw throughout my app?

I have implemented the following code which allows the user to draw on an imageView. I would like to implement this throughout my app but would prefer not to keep copying and pasting.
- (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(drawImage.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, 0.0, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), pointCurrent.x, pointCurrent.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), pointNext.x, pointNext.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
pointCurrent = pointNext;
}
So far I have tried to create a category but I'm unsure if this is the right way to go about it. Do I make a custom method and try to call that in the other classes or am I barking up the wrong tree? Thanks in advance for taking the time to read this question.
I modified your code a little, because I was getting that same error that you mention in your comment. This code worked for me.
#interface RDImageView ()
#property (nonatomic) CGPoint pointCurrent;
#end
#implementation RDImageView
-(instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
self.userInteractionEnabled = YES;
self.backgroundColor = [UIColor blueColor];
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
self.pointCurrent = [touch locationInView:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint pointNext = [touch locationInView:self];
UIGraphicsBeginImageContext(self.frame.size);
[self.image drawInRect:self.bounds];
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1,1, 0.0, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.pointCurrent.x, self.pointCurrent.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), pointNext.x, pointNext.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.pointCurrent = pointNext;
}

iOS: how to make a draw pad like draw something?

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 ....

Resources