问题
Struggling with issue since many days. Hope i can get an answer here. i have used this link to smoothen my free hand drawing. In this code i was able to set line width and color but i am finding it much difficult when i try to include undo/redo feauture in it using this link which is working fine on undo redo but its free hand dreawing is not smooth.
After some research and coding i am to know that its the cathing of drawing which is i think preventing the undo/redo.
In the first link there is a file "CachedLIView.h/m" when i used this and try to include undo/redo in this, i found that in the following method:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event // (2)
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path addLineToPoint:p];
[self drawBitmap]; // (3)
[self setNeedsDisplay];
[path removeAllPoints]; //(4)
}
this method is calling drawBitMap: method which is actually generating a temporary image every time user lifts his finger and simultaneously removing points from 'path'.
- (void)drawBitmap // (3)
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
[[UIColor blackColor] setStroke];
if (!incrementalImage) // first draw; paint background white by ...
{
UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object
[[UIColor greenColor] setFill];
[rectpath fill]; // filling it with white
}
[incrementalImage drawAtPoint:CGPointZero];
//[path stroke];
for (UIBezierPath *_path in pathArray)
[_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
i am saving every path in an array so that i can do undo/redo. (took this idea from second link).
below is the complete code of this file that i have modified to include undo/redo:
#import "CachedLIView.h"
@implementation CachedLIView
{
UIBezierPath *path;
UIImage *incrementalImage; // (1)
}
- (id)initWithFrame:(CGRect)frame // (1)
{
if (self = [super initWithFrame:frame])
{
[self setMultipleTouchEnabled:NO]; // (2)
// [self setBackgroundColor:[UIColor whiteColor]];
// path = [[UIBezierPath alloc] init];
// [path setLineWidth:3];
pathArray=[[NSMutableArray alloc]init];
bufferArray=[[NSMutableArray alloc]init];
[self drawBitmap];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
NSLog(@"in drawrect pathArray[count]: %d", pathArray.count);
[incrementalImage drawInRect:rect]; // (3)
//[[UIColor blackColor] setStroke];
//[path stroke];
for (UIBezierPath *_path in pathArray)
[_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
path = [[UIBezierPath alloc] init];
path.lineWidth = 3;
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path moveToPoint:p];
[pathArray addObject:path];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path addLineToPoint:p];
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event // (2)
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path addLineToPoint:p];
[self drawBitmap]; // (3)
[self setNeedsDisplay];
[path removeAllPoints]; //(4)
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesEnded:touches withEvent:event];
}
- (void)drawBitmap // (3)
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
[[UIColor blackColor] setStroke];
if (!incrementalImage) // first draw; paint background white by ...
{
UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object
[[UIColor greenColor] setFill];
[rectpath fill]; // filling it with white
}
[incrementalImage drawAtPoint:CGPointZero];
//[path stroke];
for (UIBezierPath *_path in pathArray)
[_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
#pragma mark - undo/redo
-(void)undoButtonClicked
{
if([pathArray count]>0){
UIBezierPath *_path=[pathArray lastObject];
[bufferArray addObject:_path];
[pathArray removeLastObject];
[self drawBitmap];
[self setNeedsDisplay];
}
}
-(void)redoButtonClicked
{
if([bufferArray count]>0){
UIBezierPath *_path=[bufferArray lastObject];
[pathArray addObject:_path];
[bufferArray removeLastObject];
[self drawBitmap];
[self setNeedsDisplay];
}
}
@end
.h file is:
#import <UIKit/UIKit.h>
@interface CachedLIView : UIView
{
NSMutableArray *pathArray;
NSMutableArray *bufferArray;
UIBezierPath *myPath;
}
-(void)undoButtonClicked;
-(void)redoButtonClicked;
@end
Please help me. What i am doing wrong. pathArray count is working correct. but not able to show undo/redo effect on screen.
回答1:
hey i can`t get your whole code but i can suggest you how can we implement such things,
For redo and undo in freehand image drawing you can capture image render
at the time of Touch began or touch end and manage stack of that renders
. Then as per your requirement(redo/undo) use that render from your cache memory. So steps are like,
1) Capture render and save when you touch your board. and manage that sequence in your cache.
- So for each touch began you have to capture image and save in sequence.
2) When you press undo at that time get your lastly render and replace that with your board.
3) For redo you can check is there any higher render are available then your current replaced render. means you can enable and disable that button by lastly updated render.
4) Not forgot to empty your cache after complete saving your image,So you can manage stack for next drawing.
- let me know if you not get this idea.
回答2:
Finally i get it and able to have smooth drawiwith undo/redo features too. It was the drawBitMap method which was causing problems..
i removed (commented) the call to this [self drawBitmap] method everywhere.
i was calling this method in touchesEnded and in undo/redo methods. I felt no need of using this method , as it does the caching (to speed up drawing ) whenever user lifts his fingere from screen a new image is created and placed on screen and old one is deleted and user is having an illusion that he is drawing continuously. But this caching is only needed in extreme situation when you really want to improve the performance (if you are going out of memory and your drawing starts becoming ugly).
So i decided to save this caching mechanism at some later stage and removed it. Though i was not able to find out why my undo/redo feature was not working with it , but i think when i had a new image cached and when it was placed on screen (when user lifts fingere) then it was creating problems as i have to retain last cached image (for undo/redo).
I will use caching at a later stage and will try to optimise my code as well.
Also remember to remove [myPath removeAllPoints]; this line from touchesEnded: method , other wise your drawing will get vanished as soon as you lift your fingere.
Hope this will help somebody. Here is the touchesEnded: modified method.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event // (2)
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path addLineToPoint:p];
//[self drawBitmap]; // CAUSES PROBLEM IF UNCOMMENTED AND UNDO/REDO WILL STOP WORKING..
[self setNeedsDisplay];
//[myPath removeAllPoints];// LINE GETS DRAWN BUT VANISHED WHEN TOUCH LIFTED OFF FROM SCREEN..
ctr = 0;
}
Later i have added this feature to SmoothedBIView and its working very fine.
来源:https://stackoverflow.com/questions/13744160/not-able-to-include-undo-redo-with-smooth-free-hand-drawing