So I'm working on an iOS app that has a GUI where you can draw on an image, add filters, etc. The drawing feature works, but will randomly cut out sometimes right at the start of drawing. Basically, if you start drawing on the screen and it works for more than half a second, your set, it will work until you try again; however sometimes when you start drawing, it will cut out after a split second for seemingly no reason.
// Draws a line from point1 to point2
- (void) drawOnImage:(CGPoint *)point1 :(CGPoint *)point2 {
UIGraphicsBeginImageContext(self.view.frame.size);
[self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), point1->x, point1->y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), point2->x, point2->y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
// Beginning of the drawing, only happens when you click but don't move around on the screen
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
if (currentAction == DRAW) {
[self backupImage];
//disable the renderImageView so that the gestures dont interfere
[self drawOnImage :&lastPoint :&lastPoint];
}
}
// When lines are being drawn on the image (when your finger is moving)
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (currentAction == DRAW) {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
[self drawOnImage :¤tPoint :&lastPoint];
lastPoint = currentPoint;
}
}
Here's my storyboard layout; as you can see, the code creates CGRects of a line as the user draws, then re-generates the main image with the CGRect as a part of the image.
Here's An example of the issue, see the two black drawing marks that ended immediately? The top draw worked and followed through, the other two immediately cut out then didn't render any other lines after that.
Any ideas why this happens? At first we thought it was the simulator, but its not.