I have paint app. When i click button it launch paint view than user can paint. After i come back then click button the paintview won't reset. How can i reset a view in button click after draw pain.
code:
- (BOOL) initContext:(CGSize)size {
int bitmapByteCount;
int bitmapBytesPerRow;
// Declare the number of bytes per row. Each pixel in the bitmap in this
// example is represented by 4 bytes; 8 bits each of red, green, blue, and
// alpha.
bitmapBytesPerRow = (size.width * 4);
bitmapByteCount = (bitmapBytesPerRow * size.height);
// Allocate memory for image data. This is the destination in memory
// where any drawing to the bitmap context will be rendered.
cacheBitmap = malloc( bitmapByteCount );
if (cacheBitmap == NULL){
return NO;
}
cacheContext = CGBitmapContextCreate (cacheBitmap, size.width, size.height, 8, bitmapBytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipFirst);
return YES;
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
[self drawToCache:touch];
[self setNeedsDisplay];
}
- (void) drawToCache:(UITouch*)touch {
hue += 0.005;
if(hue > 1.0) hue = 0.0;
UIColor *color = [UIColor colorWithHue:hue saturation:0.7 brightness:1.0 alpha:1.0];
CGContextSetStrokeColorWithColor(cacheContext, [color CGColor]);
CGContextSetLineCap(cacheContext, kCGLineCapRound);
CGContextSetLineWidth(cacheContext, 15);
CGPoint lastPoint = [touch previousLocationInView:self];
CGPoint newPoint = [touch locationInView:self];
CGContextMoveToPoint(cacheContext, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(cacheContext, newPoint.x, newPoint.y);
CGContextStrokePath(cacheContext);
CGRect dirtyPoint1 = CGRectMake(lastPoint.x-10, lastPoint.y-10, 20, 20);
CGRect dirtyPoint2 = CGRectMake(newPoint.x-10, newPoint.y-10, 20, 20);
[self setNeedsDisplayInRect:CGRectUnion(dirtyPoint1, dirtyPoint2)];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
CGImageRef cacheImage = CGBitmapContextCreateImage(cacheContext);
CGContextDrawImage(context, self.bounds, cacheImage);
CGImageRelease(cacheImage);
}
In viewcontroller code:
PaintView *paint = [[PaintView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:paint];
[paint release];
in touches moved,,,
make a toggle for eraser and change the code as follows.
if(togleEraser == NO)
{
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
}
else
{
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 20);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1, 0, 0, 10);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextClearRect (UIGraphicsGetCurrentContext(), CGRectMake(lastPoint.x, lastPoint.y, 50, 50));
}
Related
I am having an app in which I am doing some sketching on a view.
So far, it was working fine until I installed ios7.
My app uses touches moved method for recognizing the change of a movement. But when I draw a line, touches method gets called but line doesn't get updated until I touch ends in ios7.
So there is a slight lag in drawing.
It works fine on ios6 and on ios7 simulator but when i test it on a real ios7 device, there is a lag in drawing algorithm.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (!isImageSelection) {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
if ( [touch view] != baseview) {
lastPoint2 = [touch locationInView:self.viewDrawing2];
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (!isImageSelection) {
// NSLog(#"in image selection==%d touch moved ",isImageSelection );
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
// if (([touch view] != btnInkColor) || ([touch view] != btnPenSize) || ([touch view] != baseview)) {
if ( [touch view] != baseview) {
CGPoint currentPoint = [touch locationInView:self.viewDrawing];
if(isEraser) {
// [[NSUserDefaults standardUserDefaults] floatForKey:#"strokeValue"];
UIGraphicsBeginImageContext(self.viewDrawing.frame.size);
[imgDrawing.image drawInRect:CGRectMake(0, 0, self.viewDrawing.frame.size.width, self.viewDrawing.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), stroke);
//uncommented by prerak
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
imgDrawing.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
} else {
float strokeT= [[NSUserDefaults standardUserDefaults] floatForKey:#"strokeValue"];
// NSLog(#"strokeT=%f",strokeT);
UIGraphicsBeginImageContext(self.viewDrawing.frame.size);
[imgDrawing.image drawInRect:CGRectMake(0, 0, self.viewDrawing.frame.size.width, self.viewDrawing.frame.size.height)];
// NSLog(#"STROKE %f",stroke);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), strokeT);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
float redT=[[NSUserDefaults standardUserDefaults] floatForKey:#"redvalue"];
float greenT= [[NSUserDefaults standardUserDefaults] floatForKey:#"greenvalue"];
float blueT= [[NSUserDefaults standardUserDefaults] floatForKey:#"bluevalue"];
// NSLog(#"red=%f",redT);
// NSLog(#"green=%f",greenT);
// NSLog(#"blue=%f",blueT);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), redT, greenT, blueT, 1.0);
CGContextSetRGBFillColor(UIGraphicsGetCurrentContext(), redT, greenT, blueT, 1.0);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineJoin(UIGraphicsGetCurrentContext(), kCGLineJoinRound);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
imgDrawing.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
lastPoint = currentPoint;
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
//if (([touch view] != btnInkColor) || ([touch view] != btnPenSize) || ([touch view] != baseview)) {
if ( [touch view] != baseview) {
if (!isImageSelection) {
if(!mouseSwiped) {
if (isEraser) {
UIGraphicsBeginImageContext(self.viewDrawing.frame.size);
[imgDrawing.image drawInRect:CGRectMake(0, 0, self.viewDrawing.frame.size.width, self.viewDrawing.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), stroke);
// CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
imgDrawing.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
} else {
UIGraphicsBeginImageContext(self.viewDrawing.frame.size);
[imgDrawing.image drawInRect:CGRectMake(0, 0, self.viewDrawing.frame.size.width, self.viewDrawing.frame.size.height)];
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), stroke);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetRGBFillColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineJoin(UIGraphicsGetCurrentContext(), kCGLineJoinRound);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
imgDrawing.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
}
}
}
}
How can i solve this?
To find a instant solution, Replace this line
mainImage.image = UIGraphicsGetImageFromContext(UIGraphicsGetCurrentContext());
with
[mainImage performSelectorInBackground:#selector(setImage:) withObject:UIGraphicsGetImageFromCurrentImageContext()];
If you need an elaborate and accurate solution try to replace the MoveTo with CGMutablepath .
Hope this helps.
we have solved this problem using dispatch_async(dispatch_get_main_queue(), ^{)}; block in touchMoved method like this :
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
dispatch_async(dispatch_get_main_queue(), ^{
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
if(touch.view==self.vwSwipePage)
{
return;
}
if(flagIsEraser)
{
CGPoint currentPoint;
if(flagActiveViewPage)
{
currentPoint = [touch locationInView:self.mainImage];
UIGraphicsBeginImageContext(self.mainImage.frame.size);
[self.mainImage.image drawInRect:CGRectMake(0, 0, self.mainImage.frame.size.width, self.mainImage.frame.size.height)];
}
else
{
currentPoint = [touch locationInView:self.mainImage1];
UIGraphicsBeginImageContext(self.mainImage1.frame.size);
[self.mainImage1.image drawInRect:CGRectMake(0, 0, self.mainImage1.frame.size.width, self.mainImage1.frame.size.height)];
}
//clear using circle brush........
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context,20);
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextSetStrokeColorWithColor(context, [[UIColor clearColor] CGColor]);
CGContextBeginPath(context);
CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
CGContextStrokePath(context);
CGContextFlush(context);
if(flagActiveViewPage)
self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext();
else
self.mainImage1.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
});
}
May this will Help u to solve ur problem.
Thanks
Here's listed same code with same problem on ios7 https://stackoverflow.com/questions/18198129/ios-7-making-my-apps-drawing-algorithm-lag people suggest moving drawing logic into drawRect:
I have UIView subclass and I want to draw a CGPath out of the drawRect. I have heard that it is and isn't possible. I understand that the CGPath needs context. I tried to give it context with the newImage. I am also thinking that I have to call this differently for example my code in a CCLayer for the view to display what would be in the drawRect
- (void)createPath:(CGPoint)origin
{
CGSize size=CGSizeMake(320, 480);
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsBeginImageContextWithOptions(size,YES,1.0f);
CGContextSaveGState(context);
UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext();
CGRect newRect= CGRectMake(0, 0, 320, 480);
CGMutablePathRef path1 = CGPathCreateMutable();
CGPathMoveToPoint(path1, nil,100, 200);
CGPoint newloc = CGPointMake(300, 130);
CGPathMoveToPoint(path1, NULL, newloc.x, newloc.y);
CGPathAddLineToPoint(path1, NULL, newloc.x + 16,newloc.y + 38);
CGPathAddLineToPoint(path1, NULL, newloc.x + 49, newloc.y + 0);
CGPathAddLineToPoint(path1, NULL, newloc.x + 23, newloc.y - 39);
CGPathAddLineToPoint(path1, NULL, newloc.x - 25,newloc.y - 40);
CGPathAddLineToPoint(path1, NULL, newloc.x -43, newloc.y + 0);
CGPathCloseSubpath(path1);
CGContextAddPath(context, path1);
CGContextSaveGState(context);
CGContextSetLineWidth(context, 10.0);
CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
//Fill and stroke
CGContextDrawPath(context, kCGPathFillStroke);
//CGContextDrawImage(context, newRect, myImage.CGImage);
UIGraphicsEndImageContext();
CGContextRestoreGState(context);
CGPathRelease(path1);
CGContextRelease(context);
}
See if this helps you. Have a UIView with UIImageView as it's subview. Then implement the touches method as below:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
//Get touch point
CGPoint currentPoint = [touch locationInView:drawImage];
UIGraphicsBeginImageContext(self.frame.size);
//drawImage is the UIImageView
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
//Line attributes
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.5, 1.0, 1.0);
//Begin path
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGPathMoveToPoint(drawPath, NULL, lastPoint.x, lastPoint.y);
CGPathAddLineToPoint(drawPath, NULL, currentPoint.x, currentPoint.y);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
Now you have the UIImageView visible with what you drew and drawPath will have your path.
Actually i got few samples for drawing image through free hand and i integrated as well in my application.I need additional functionalities such as undo/redo.How can i achieve this?Need help on this..I used following code.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y -= 20;
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 10) {
mouseMoved = 0;
}
}
CGMutablePathRef path;
path = CGPathCreateMutable();
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGPathMoveToPoint(path, NULL, previousPoint.x, previousPoint.y);
CGPathAddLineToPoint(path, NULL, lastPoint.x, lastPoint.y);
CGContextAddPath(context, path);
CGContextSetLineWidth(context, 10.0);
[[UIColor greenColor] setStroke];
CGContextDrawPath(context,kCGPathFillStroke);
}
Now you can perform Undo/Redo by path's index.
You can try storing all paths in an array.
When you draw a new one, add it last and draw it, when you delete - remove the last and redraw all. Keep removed in another array so you can redo actions, clear the redo array when a new path is added to the paths array.
That's the most straight-forward method I can think of.
I have successfully implemented touchesMoved and it is drawing line properly now I am facing problem in implementing erase method, please assist me where I am lacking.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y -= 20;
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 10) {
mouseMoved = 0;
}
}
- (void) Erase
{
//NSSet *touches;
//UITouch *touch; //= (NSSet *)[touch anyObject];
CGPoint currentPoint; //= [touch locationInView:self.view];
currentPoint.y += 20;
if(mouseSwiped) {
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
mouseMoved--;
}
I have implemented my erases successfully in touches moved but when I tap one so it erases but on mouse over and tap left it stop erasing. Then need to click(tap) again for erasing at new point. So how can I continously erases on touches moved,
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (eraseBtnSelected == YES) {
if ([touch tapCount] == 1) {
[self EraseButton];
}
}
}
Don't do any drawing in your event-handling methods. Ever. Use them to edit a list of things to draw, and do all of your drawing in -draw... methods like -drawRect:.
To erase something, just delete it from your list of things to draw, and redraw the view or layer in question.
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 1.0, 1.0, 1.0);
this line of code works if background colour is white if other then it is a problem
change following line
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
to
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 1.0, 1.0, 1.0);
I want to draw nil instead of the colors, because in the end I want the code to erase a section of the UIImageView in order that you can see what is behind the UIImageView.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.x = currentPoint.x;
currentPoint.y = currentPoint.y;
mouseSwiped = YES;
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushSizeVal);
CGContextSetAlpha(UIGraphicsGetCurrentContext(), drawingColorAlpha);
CGContextSaveGState(UIGraphicsGetCurrentContext()); //Probably right here
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),
drawingColorRed, // I want to draw nil here instead of these CGFloats that I have made
drawingColorGreen, // I want to draw nil here instead of these CGFloats that I have made
drawingColorBlue, // I want to draw nil here instead of these CGFloats that I have made
drawingColorAlpha); // I want to draw nil here instead of these CGFloats that I have made
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(),
lastPoint.x,
lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),
currentPoint.x,
currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); //drawImage is the UIImageView that I am drawing my context to
NSLog(#"current point x: %d current point y: %d",currentPoint.x, currentPoint.y); //Used for my purposes
lastPoint = currentPoint;
mouseMoved++;
and
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!optionsDisplayerVisible && canDraw)
{
if(!mouseSwiped)
{
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushSizeVal);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),
drawingColorRed,
drawingColorGreen,
drawingColorBlue,
drawingColorAlpha);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); //drawImage is the UIImageView that I am drawing to
UIGraphicsEndImageContext();
}
}
}
Colin Gislason's answer is incorrect. If you set drawingColorAlpha to 0, it just paints an invisible color on the existing image.
Here is what works.
//set this at the beginning
CGContextSetLineCap(ctx,kCGImageAlphaNone);
//after moving the point to your pen location:
CGContextClearRect (ctx, CGRectMake(lastPoint.x, lastPoint.y, 10, 10));
What you want to do is set drawingColorAlpha to 0. This creates a transparent color which is equivalent to erasing. You might also have to set the blend mode using CGContextSetBlendMode(). kCGBlendModeNormal should work.
Set blend mode as follows:
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
Hope it will help u !! :P
What you want is not nil, but a color that's transparent. If your UIImageView isn't marked as "Opaque" in IB, then it should show through in any areas where the pixels are transparent.