Reputation: 4636
I am working on Drawing in iOS, I have my code working, drawing is working fine, Now I have implemented undo and redo functions, but what is happening is that, suppose I draw two lines and then press undo button, then the first line drawn is getting Blur, and I am not understanding why its happening, below is my undo code.
-
(void)redrawLine
{
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
NSDictionary *lineInfo = [lineArray lastObject];
curImage = (UIImage*)[lineInfo valueForKey:@"IMAGE"];
UIGraphicsEndImageContext();
[self setNeedsDisplayInRect:self.bounds];
[self setNeedsDisplay];
}
-(void)undoButtonClicked
{
if([lineArray count]>0){
NSMutableArray *_line=[lineArray lastObject];
[bufferArray addObject:[_line copy]];
[lineArray removeLastObject];
drawStep = UNDO;
[self redrawLine];
}
}
- (void)drawRect:(CGRect)rect
{
switch (drawStep) {
case DRAW:
{
[curImage drawAtPoint:CGPointMake(0, 0)];
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.layer renderInContext:context];
CGContextMoveToPoint(context, mid1.x, mid1.y);
CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context, kCGLineCapButt);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextSetAlpha(context, self.lineAlpha);
CGContextSetAllowsAntialiasing(context, YES);
CGContextStrokePath(context);
[super drawRect:rect];
}
case UNDO:
{
[curImage drawInRect:self.bounds];
break;
}
}
Below is the image of what is happening when undo button is pressed
First Image is before undobutton is clicked and second Image is after undobutton is clicked
Regards Ranjit
Upvotes: 2
Views: 1672
Reputation: 2488
Your call to UIGraphicsBeginImageContext
is specifying the size of the bitmap in points (i.e. 320x480 for full-screen), but on a device with a retina display, you'd need one that's the size of the screen in pixels (i.e. 640x960).
You should be able to call UIGraphicsBeginImageContextWithOptions
with scale
set to 0.0. From the docs:
The scale factor to apply to the bitmap. If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen.
Upvotes: 5