how to fix "Reference to 'kCGLineCapRound' is ambiguous" error in Xcode - ios

This is the code and I am receiving the error that: Reference to 'kCGLineCapRound' is ambiguous
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentLocation = [touch locationInView:self];
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(ctx,kCGLineCapRound);
CGContextSetLineWidth(ctx, 5.0);
CGContextSetRGBStrokeColor(ctx, 0.0, 0.0, 1.0, 1.0);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, location.x, location.y);
CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
CGContextStrokePath(ctx);
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
location = currentLocation;
}
I don't know how to get rid of this error, any help is appreciated!
Thanks in advance!

I think you need to add CoreGraphics.framework to you project.
http://docs.millennialmedia.com/iOS-SDK/iOSAddingFrameworks.html

Related

drawing image laggy on iPhone plus

I want to draw lines on a view, here's my code.
- (void)drawRect:(CGRect)rect {
[self.image drawInRect:self.bounds];
}
- (void)drawNewLine {
UIGraphicsBeginImageContext(self.bounds.size);
[self.image drawInRect:self.bounds];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetRGBStrokeColor(ctx, 0.51, 0.85, 0.81, 1);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 4.0);
CGContextBeginPath(ctx);
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
CGContextMoveToPoint(ctx, mid1.x, mid1.y);
CGContextAddQuadCurveToPoint(ctx, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self setNeedsDisplay];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[recognizer resetTouches];
[recognizer addTouches:touches fromView:self];
self.image = nil;
previousPoint1 = previousPoint2 = currentPoint = [[touches anyObject] locationInView:self];
[self setNeedsDisplay];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
previousPoint2 = previousPoint1;
previousPoint1 = currentPoint;
currentPoint = [[touches anyObject] locationInView:self];
[self drawNewLine];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self drawNewLine];
[self processGestureData];
}
It worked fine on other devices except iPhone plus, on 6/6s/7 plus, you can see the clear lag. Can someone please tell me where I did wrong. Thanks a lot.
Here's my app. CallHoney

A specific drawing field in a viewcontroller - Objective C

I am making an application where the user sign an form with their signature (analog, with stylus) and I am having this code:
In .h:
CGPoint lastPoint;
CGPoint movebackTo;
CGPoint currentPoint;
CGPoint location;
NSDate *lastClick;
BOOL mouseSwiped;
UIImageView *drawImage;
And in .m
- (void)viewDidLoad {
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
drawImage.image = [preferences objectForKey:#"drawImageKey"];
drawImage = [[UIImageView alloc] initWithImage:nil];
drawImage.frame = CGRectMake(0, 0, 768, 1024);
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([touch tapCount] == 3) {
drawImage.image = nil;
}
location = [touch locationInView:self.view];
lastClick = [NSDate date];
lastPoint = [touch locationInView:self.view];
lastPoint.y -= 0;
[super touchesBegan: touches withEvent: event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:self.view];
UIGraphicsBeginImageContext(CGSizeMake(768, 1024));
[drawImage.image drawInRect:CGRectMake(0, 0, 768, 1024)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.8, 0.8, 0.8, 1);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
[drawImage setFrame:CGRectMake(0, 0, 768, 1024)];
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
[self.view addSubview:drawImage];
}
The problem is, this works fine if I want to use the WHOLE screen for drawing. But I want to use a specific part of the view as "drawing box. I already tried things like changing the line: [drawImage.image drawInRect:CGRectMake(0, 0, 768, 1024)]; to CGRectMake(58, 623, 240, 143)]; Which is the size and position the box has to be, but this is not working for me.
What happens when i change those kind of things: i can draw in the leftcorner of the view, but the actual lines display in the right box! But the drawing, plus the displaying of the code has to be in the box which is located at: X-58 y-623 and is W240 and H143.
I've searched the whole web for this specific thing but I could not find a good answer for this.
I hope you guys can help me out! It would be appreciated very much!

Draw on UIImageView over MKMapView

I have searched a lot an answer for this question, but none of them seem to do exactly what I want. A lot of tutorials show me how to add lines and polygons in code, but not with freehand drawing.
I got a MKMapView with a UIImageView over the map. And I can draw a little on my UIImageView but right after that, the MKMapView is draged and the draw doesn't continue.
I would like to know what have I done wrong ?
Here is my code for the touches events :
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
pointA = [touch locationInView:drawView];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentLocation = [touch locationInView:drawView];
UIGraphicsBeginImageContext(drawView.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[drawView.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)];
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, 5.0);
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, pointA.x, pointA.y);
CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
CGContextStrokePath(ctx);
drawView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
pointA = currentLocation;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentLocation = [touch locationInView:drawView];
UIGraphicsBeginImageContext(drawView.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[drawView.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)];
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, 5.0);
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, pointA.x, pointA.y);
CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
CGContextStrokePath(ctx);
drawView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
pointA = currentLocation;
}
And the code of the button who show the UIImageView :
EDIT : I made this for stoping the interactions with the map :
- (IBAction)modeDraw:(id)sender {
if (drawView.alpha == 0.0) {
drawView.image=nil; //empty for a new draw
drawView.backgroundColor = [UIColor whiteColor];
[UIImageView beginAnimations:nil context:nil];
[UIImageView setAnimationDuration:0.5];
[drawView setAlpha:0.4];
[UIImageView commitAnimations];
myMapView.zoomEnabled = NO;
myMapView.scrollEnabled = NO;
} else {
[UIImageView beginAnimations:nil context:nil];
[UIImageView setAnimationDuration:0.5];
[drawView setAlpha:0.0];
[UIImageView commitAnimations];
myMapView.zoomEnabled = YES;
myMapView.scrollEnabled = YES;
}
}
After doing my draw, I would like to transform this draw into a form (area), that will be put on the map. If you have some indications for me ?
Thank you, and sorry for my bad english.
Here is my code if anyone have the same problem as me and the link that Allan gave me : http://www.apps168.net/post/1460.html :
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
pointA = [touch locationInView:drawView];
pathRef = CGPathCreateMutable();
CGPathMoveToPoint(pathRef, NULL, pointA.x, pointA.y);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentLocation = [touch locationInView:drawView];
CGPoint pastLocation = [touch previousLocationInView:drawView];
UIGraphicsBeginImageContext(drawView.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[drawView.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)];
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, 5.0);
CGContextSetRGBStrokeColor(ctx, 0.0, 0.0, 0.0, 1.0);
CGContextBeginPath(ctx);
CGContextStrokePath(ctx);
CGContextMoveToPoint(ctx, pastLocation.x, pastLocation.y);
CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
CGContextDrawPath(ctx, kCGPathFillStroke);
drawView.image=UIGraphicsGetImageFromCurrentImageContext();
CGPathAddLineToPoint(pathRef, NULL, currentLocation.x, currentLocation.y);
UIGraphicsEndImageContext();
//pointA = currentLocation;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGPathCloseSubpath(pathRef);
NSUInteger count = [array count];
for (NSUInteger i = 0; i < count; i=i+2) {
NSString *lat = [array objectAtIndex: i];
NSString *lgt = [array objectAtIndex: i+1];
CLLocationCoordinate2D pointLoc;
pointLoc.latitude = [lat doubleValue];
pointLoc.longitude = [lgt doubleValue];
locationConverToImage = [myMapView convertCoordinate:pointLoc toPointToView:drawView];
if (CGPathContainsPoint(pathRef, NULL, locationConverToImage, NO)) {
NSLog(#"point in path!");
//sélection de ces points et cacher les autres.
}
}
[UIImageView beginAnimations:nil context:nil];
[UIImageView setAnimationDuration:0.5];
[drawView setAlpha:0.0];
[UIImageView commitAnimations];
myMapView.zoomEnabled = YES;
myMapView.scrollEnabled = YES;
}
Draw my map try this , this is draw map point project

Undo and Redo functionality in free hand drawing application

I working on a drawing app, and I am able to draw with my finger touch, now I am trying to implement, clear ,undo and redo functionality,In my viewcontroller ,I have two IBAction methods for "clearAll" and "Undo", I have created a custom class called drawing.h and .m where I have written functions for handling touch events,below are my functions..
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
tempArray = [[NSMutableArray alloc]init];
if ([touch tapCount] == 2)
{
self.image = nil;
}
return;
}
currentlocation.location = [touch locationInView:self];
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
PointLocation *currentLocation1 = [[PointLocation alloc] init];
currentLocation1.Location = [touch locationInView:self];
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, 2.0);
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, currentlocation.Location.x, currentlocation.Location.y);
CGContextAddLineToPoint(ctx, currentLocation1.Location.x, currentLocation1.Location.y);
CGContextStrokePath(ctx);
CGContextSetFlatness(ctx, 0.1);
CGContextSetAllowsAntialiasing(ctx, true);
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
currentlocation = currentLocation1;
**[tempArray addObject:[NSArray arrayWithObjects:currentlocation,currentLocation1, nil]];
NSLog(#"%i",[tempArray count]);**
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
PointLocation *currentLocation1 = [[PointLocation alloc] init];
currentLocation1.Location = [touch locationInView:self];
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, 2.0);
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, currentlocation.Location.x, currentlocation.Location.y);
CGContextAddLineToPoint(ctx, currentLocation1.Location.x, currentLocation1.Location.y);
CGContextStrokePath(ctx);
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
currentlocation = currentLocation1;
**[tempArray addObject:[NSArray arrayWithObjects:currentlocation,currentLocation1, nil]];
NSLog(#"%i",[tempArray count]);**
}
In the above code I am adding all the CGPoints to an Array,the array is filling with the points, now how shall I perform the undo functionality?
So friends please help me out..
Regards
Ranjit

doodle on UIView - ios development

Hello I am working on this project to allow user doodle on a UIView. My approach is creating an CGMutablePathRef path and adding new lines to it while TouchMoved.
The codes relevant are listed below, which are in class of UIView.
static CGMutablePathRef path; //create it as static value.
//I got this habit from java coding but actually not sure
//if it's a good way to do it in objective-c.
//draw the path
-(void)drawRect:(CGRect)rect{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextAddPath(context, path);
CGContextStrokePath(context);
CGPathRelease(path);
}
//touch began. create the path and add point.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
path = CGPathCreateMutable();
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
CGPathMoveToPoint(path, NULL, point.x, point.y);
[self setNeedsDisplay];
}
//add points when touch moved
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
CGPathAddLineToPoint(path, NULL, point.x, point.y);
[self setNeedsDisplay];
}
//last points when touch ends
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
CGPathAddLineToPoint(path, NULL, point.x, point.y);
[self setNeedsDisplay];
}
However, I got errors and I didn't really understand them... I guess it's something with UIGestureRecognizer, which I saw from other doodle codes. Is it necessary to add UIGestureRecognizer to it? I haven't learned anything about it so I was trying to avoid using it. But if it's a must, I will give a try.
Another approach I am thinking of is to store all the point positions to an mutable array, because I have to store these point position values somewhere. However, I don't know if array is suitable because I haven't find a way to store floating values to the array. It will be greatly helpful if anyone can help me with this. Thank you!
Thanks to guys who offered me help. After looking thru a bunch of codes and approaches, luckily I found the effective way to solve this problem!
Generally, the imageView in my SketchView is updated dynamically when I am drawing. That is, every time I drew more pixel, one line will be added to that new pixel, change the UIImageView and then set it as the background image of UIView.
SketchView.h
#property (assign, nonatomic) CGPoint CurrentPoint;
#property (assign, nonatomic) CGPoint PreviousPoint;
#property (assign, nonatomic) CGPoint InitialPoint;
SketchView.m
#synthesize CurrentPoint;
#synthesize PreviousPoint;
#synthesize InitialPoint;
//begin the touch. store the initial point because I want to connect it to the last
//touch point
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:image];
InitialPoint = point;
}
//When touch is moving, draw the image dynamically
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
PreviousPoint = [touch previousLocationInView:image];
CurrentPoint = [touch locationInView:image];
UIGraphicsBeginImageContext(image.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[image.image drawInRect:CGRectMake(0, 0, image.frame.size.width, image.frame.size.height)];
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, 5.0);
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, PreviousPoint.x, PreviousPoint.y);
CGContextAddLineToPoint(ctx, CurrentPoint.x, CurrentPoint.y);
CGContextStrokePath(ctx);
image.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
PreviousPoint = [touch previousLocationInView:image];
CurrentPoint = [touch locationInView:image];
UIGraphicsBeginImageContext(image.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[image.image drawInRect:CGRectMake(0, 0, image.frame.size.width, image.frame.size.height)];
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, 5.0);
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, PreviousPoint.x, PreviousPoint.y);
CGContextAddLineToPoint(ctx, CurrentPoint.x, CurrentPoint.y);
//I connected the last point to initial point to make a closed region
CGContextMoveToPoint(ctx, CurrentPoint.x, CurrentPoint.y);
CGContextAddLineToPoint(ctx, InitialPoint.x, InitialPoint.y);
CGContextStrokePath(ctx);
image.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
And it works!
p.s. I found
PreviousPoint = [touch previousLocationInView:image];
very helpful, though it's not mentioned much in the codes I found doing doodling... I hope this helps. :)

Resources