I am trying an image erasing code in my PanGesture. I am using the code below:
UIImage * image = [UIImage imageNamed:#"326d4fb72362a2e44659141cadfc4977.jpg"];
holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 235)];
imgForeground = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imgForeground setImage:image];
[holderView addSubview:imgForeground];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
[self.view addSubview:holderView];
Then I am handling this here:
-(void)move:(id)sender {
CGPoint currentPoint;
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan){
lastPoint = [sender locationInView:imgForeground];
lastPoint.y -=20;
}
else{
currentPoint = [sender locationInView:imgForeground];;
currentPoint.y -=20;
}
UIGraphicsBeginImageContext(imgForeground.frame.size);
[imgForeground.image drawInRect:CGRectMake(imgForeground.frame.origin.x, imgForeground.frame.origin.y, imgForeground.frame.size.width, imgForeground.frame.size.height)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineCap(context,kCGLineCapRound); //kCGImageAlphaPremultipliedLast);
CGContextSetLineWidth(context, 10);
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
CGContextBeginPath(context);
CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(context, lastPoint.x, lastPoint.y);
CGContextStrokePath(context);
imgForeground.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
How to unerase the same image?
I have the same issue in my app after the long search i found the simple solution for this issue.
i just used touches methods of the UIViewController
The below is the my approach,
Code:-
#pragma mark touches stuff...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
lastTouch = [touch locationInView:self.editedImageView];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
currentTouch = [touch locationInView:self.editedImageView];
CGFloat brushSize;
if (isEraser)
{
brushSize=eraser;
}
else
{
brushSize=mark;
}
CGColorRef strokeColor = [UIColor whiteColor].CGColor;
UIGraphicsBeginImageContext(self.editedImageView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.editedImageView.image drawInRect:CGRectMake(0, 0, self.editedImageView.frame.size.width, self.editedImageView.frame.size.height)];
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, brushSize);
if (isEraser) {
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:self.im].CGColor);
}
else
{
CGContextSetStrokeColorWithColor(context, strokeColor);
CGContextSetBlendMode(context, kCGBlendModeClear);
}
CGContextBeginPath(context);
CGContextMoveToPoint(context, lastTouch.x, lastTouch.y);
CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
CGContextStrokePath(context);
self.editedImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastTouch = [touch locationInView:self.editedImageView];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
The Inputs:
self.editedImageView.image = "Your Custom image";
self.im = "Your Custom image";
The simple solution of your problem will be :-
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:self.im].CGColor);
Note:
This not unerase it again drawing the image over
Update:-
self.im = "Your Custom image";
this will be like this....
-(void)eraserConfigure
{
UIImageView *resizeImage=[[[UIImageView alloc]initWithImage:editedImage]autorelease];
/*UIImage */self.im=[UIImage imageFromView:resizeImage scaledToSize:CGSizeMake(self.editedImageView.frame.size.width, self.editedImageView.frame.size.height)];
}
Save a temporary image after every drawing operation.
You should save on temporary files and limit the number of undo operations.
Related
Hi Iam working with UIPopOverViewController in ipad app. I called touches event methods, off course those are automatically called when touch. But those are UIView methods. My problem is I have popOverViewController, in that image-view. when i touch in that image-view, delegate methods are not called.
I do it in another way, it is i sub-view the image view in UIView, in this case delegates are called. when i assigned that image-view to PopOverView, delegates are not called.
here is the sample code :
if(popOverViewBool) {
signatureImageView.image=nil;
[currentTextField resignFirstResponder];
[currentTextView resignFirstResponder];
red = 0.0/255.0;
green = 0.0/255.0;
blue = 0.0/255.0;
brush = 2.2;//10.0
opacity = 1.0;
UIViewController* popOverVCont = [[UIViewController alloc] init];
popOverVCont.view=myView;
popOverVCont.contentSizeForViewInPopover =CGSizeMake(590, 350);
self.popoverController = [[UIPopoverController alloc]
initWithContentViewController:popOverVCont];
popoverController.delegate = self;
[popoverController presentPopoverFromRect:CGRectMake(-137, 460, 320, 10) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
popOverViewBool=NO;
}
else
{
[popoverController presentPopoverFromRect:CGRectMake(-137, 460, 320, 10) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
//lastPoint = [touch locationInView:self.view];
lastPoint = [touch locationInView:self.drawSignView];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
//CGPoint currentPoint = [touch locationInView:self.view];
CGPoint currentPoint = [touch locationInView:self.drawSignView];
//UIGraphicsBeginImageContext(self.view.frame.size);
UIGraphicsBeginImageContext(self.drawSignView.frame.size);
//[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.drawSignView.frame.size.width, self.drawSignView.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempDrawImage setAlpha:opacity];
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(!mouseSwiped)
{
//UIGraphicsBeginImageContext(self.view.frame.size);
UIGraphicsBeginImageContext(self.drawSignView.frame.size);
//[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.drawSignView.frame.size.width, self.drawSignView.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
UIGraphicsBeginImageContext(self.mainImage.frame.size);
//[self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
[self.mainImage.image drawInRect:CGRectMake(0, 0, self.drawSignView.frame.size.width, self.drawSignView.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
//[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:opacity];
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.drawSignView.frame.size.width, self.drawSignView.frame.size.height) blendMode:kCGBlendModeNormal alpha:opacity];
self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext();
self.tempDrawImage.image = nil;
UIGraphicsEndImageContext();
}
i Google it, but i did not find any answer..
Please give any ideas or suggestions.
Thanks
If you want to use touches* series in your view, you gotta make custom class that implements that methods.
In your case, you have to make SomeImageView class which is subclass of UIImageView.
Or you can use subclasses of UIGestureRecognizer like UITapGestureRecognizer or UIPinchGestureRecognizer. in your viewDidLoad of controller,
self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
self.tapGestureRecognizer.cancelsTouchesInView = NO;
self.tapGestureRecognizer.delegate = self;
self.tapGestureRecognizer.numberOfTapsRequired = 1; // invoked at 1 tap.
self.photoScaleFrameView addGestureRecognizer:self.tapGestureRecognizer];
and in your controller.m
- (void)handleTap:(UITapGestureRecognizer*)recognizer {
NSLog(#"tap gesture detected");
}
and bam.
How can I clear a drawing in a subview? What should I put in my (void)clearLine below?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Setup subview to draw a line
CGRect subviewRect = CGRectMake(0, 0, 250, 300);
Draw* _draw = [[Draw alloc] initWithFrame:subviewRect];
_draw.exclusiveTouch = NO;
_draw.backgroundColor = [UIColor clearColor];
[self addSubview:_draw];
}
return self;
}
- (void) clearLine
{
// how can I clear the drawing in Draw
}
And below is the Draw.m file, that will draw a straight line by getting the first touch as the starting point and the second touch as the ending point.
#implementation Draw
- (void)drawRect:(CGRect)rect {
CGPoint fromPoint;
CGPoint toPoint;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
}
// Touch event
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touchPoint = [touches anyObject];
fromPoint = [touchPoint locationInView:self];
toPoint = [touchPoint locationInView:self];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
toPoint = [touch locationInView:self];
[self setNeedsDisplay];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
toPoint = [touch locationInView:self];
[self setNeedsDisplay];
}
In clear line you should remove Draw view and set it nil
Draw* _draw;
- (void) clearLine
{
[_draw removeFromSuperview];
_draw=nil;
}
and to again enable drawing use
-(void) initDrawView
{
CGRect subviewRect = CGRectMake(0, 0, 250, 300);
if(_draw!=nil)
{
[_draw removeFromSuperview];
_draw=nil;
}
_draw = [[Draw alloc] initWithFrame:subviewRect];
_draw.exclusiveTouch = NO;
_draw.backgroundColor = [UIColor clearColor];
[self addSubview:_draw];
}
Draw the background color on your view,
Like if you have white background then [UIColor whiteColor];
if you have black background then [UIColor blackColor];
Make a Bool variable in Draw class
And when you want to clear all like this
- (void) clearLine
{
// Here you can clearn lines
[_draw CleanAllLines]
}
And In Draw class make the method
-(void)CleanAllLines{
isNeedToClear = YES;
[self setNeedsLayout];
}
- (void)drawRect:(CGRect)rect {
if(isNeedToClear){
isNeedToClear = NO;
return;
}
CGPoint fromPoint;
CGPoint toPoint;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
}
I have created a custom UIView (without .xib) for a finger paint application.
Paint is working fine with custom UIView but my problem is that when I try to erase the painted path I am getting:
Error : Invalid context
Below is my class:
.h file
#interface draw2D : UIView
{
CGPoint previousPoint;
CGPoint lastPoint;
CGMutablePathRef path;
UIButton *btnClose;
UIButton *btnErase;
BOOL IsErase;
}
- (IBAction)btnClose:(id)sender;
- (IBAction)btnErase:(id)sender;
#end
#implementation draw2D
- (void)awakeFromNib
{
path = CGPathCreateMutable();
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
btnClose = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnClose addTarget:self action:#selector(btnClose:)
forControlEvents:UIControlEventTouchDown];
[btnClose setTitle:#"close" forState:UIControlStateNormal];
btnClose.frame = CGRectMake(10, 10, 100, 40.0);
btnErase = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnErase addTarget:self action:#selector(btnErase:)
forControlEvents:UIControlEventTouchDown];
[btnErase setTitle:#"Erase" forState:UIControlStateNormal];
btnErase.frame = CGRectMake(150, 10, 100, 40.0);
[self addSubview:btnClose];
[self addSubview:btnErase];
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
NSLog(#"Touch Began :%d",[touch tapCount]);
if ([touch tapCount] > 1)
{
NSLog(#"::::: Paint Start :::::");
path = CGPathCreateMutable();
previousPoint = lastPoint;
[self setNeedsDisplay];
}
self.backgroundColor = [UIColor clearColor];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"::::: touchesMoved :::::");
lastPoint = [[touches anyObject] locationInView:self];
previousPoint = [[touches anyObject] previousLocationInView:self];
if(IsErase)
{
NSLog(#"erase");
UITouch *erasetouch = [touches anyObject];
CGPoint erasecurrentPoint = [erasetouch locationInView:self];
CGContextRef erasecontext = UIGraphicsGetCurrentContext();
CGContextSetLineCap(erasecontext, kCGLineCapRound);
CGContextSetLineWidth(erasecontext,10);
CGContextSetBlendMode(erasecontext, kCGBlendModeClear);
CGContextSetStrokeColorWithColor(erasecontext, [[UIColor clearColor] CGColor]);
CGContextBeginPath(erasecontext);
CGContextMoveToPoint(erasecontext, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(erasecontext, erasecurrentPoint.x, erasecurrentPoint.y);
CGContextStrokePath(erasecontext);
CGContextFlush(erasecontext);
}
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
NSLog(#"::::: drawRect :::::");
CGContextRef context = UIGraphicsGetCurrentContext();
CGPathMoveToPoint(path, NULL, previousPoint.x, previousPoint.y);
CGPathAddLineToPoint(path, NULL, lastPoint.x, lastPoint.y);
CGContextAddPath(context, path);
CGContextSetLineWidth(context, 5);
[[UIColor blueColor] setStroke];
CGContextDrawPath(context, kCGPathFillStroke);
}
- (IBAction)btnClose:(id)sender
{
[self removeFromSuperview];
}
- (IBAction)btnErase:(id)sender
{
IsErase = YES;
}
#end
I have set erase button with functionality but not working.
The problem you have is that you are not supposed to call UIGraphicGetContext() outside of drawRect:
In your touchesBegan:withEvent: and touchesMoved:withEvent: you should simply store the points you want to draw and call [self setNeedsDisplay] as you are doing now.
In your drawRect: implementation you would then draw the points you have stored.
You can take a look at this github repo that provides an implementation for a smooth drawing:https://github.com/levinunnink/Smooth-Line-View
finally i have find the solution . my mistake was i m implement erase code intouchMove method with new context . i dont have to need new context . implement erase code in drawrect method and now its working fine . see the below code.
- (void)drawRect:(CGRect)rect
{
[curImage drawAtPoint:CGPointMake(0, 0)];
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
context = UIGraphicsGetCurrentContext();
[self.layer renderInContext:context];
if(IsErase)
{
CGContextSetLineWidth(context,self.lineWidth);
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextSetStrokeColorWithColor(context, [[UIColor clearColor] CGColor]);
CGContextBeginPath(context);
CGContextMoveToPoint(context, mid1.x, mid1.y);
CGContextAddLineToPoint(context, previousPoint1.x, previousPoint1.y);
CGContextStrokePath(context);
CGContextFlush(context);
}
else
{
CGContextMoveToPoint(context, mid1.x, mid1.y);
CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextSaveGState(context);
CGContextStrokePath(context);
}
[super drawRect:rect];
[curImage release];
}
i hope it will helps to someone in erase functionality .
I am working on my application, I want to hide the bottom toolbar so I can draw where that is, but I can't figure out how to do this.
Also, I used a tutorial to help with the drawing, but I can't figure out why it is stopping the drawing at the top of the screen.
- (void)viewDidLoad {
[super viewDidLoad];
drawImage = [[UIImageView alloc] initWithImage:nil];
drawImage.frame = self.view.frame;
[self.view addSubview:drawImage];
self.view.backgroundColor = [UIColor lightGrayColor];
mouseMoved = 0;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
drawImage.image = nil;
return;
}
lastPoint = [touch locationInView:self.view];
lastPoint.y -= 20;
}
- (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)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
drawImage.image = nil;
return;
}
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();
}
}
In your viewDidLoad you have:
drawImage.frame = self.view.frame;
To get rid of the gap at the top of your drawing, you should actually have:
drawImage.frame = self.view.bounds;
To understand why this works, you need to understand what the meaning of frame versus bounds is. See perhaps this question: Do I have the right understanding of frames and bounds in UIKit?
As for getting rid of the toolbar -- you need to tell us more about how your UI is set up. Is the view in your screenshot set up in interface builder?
I have this code that's running really slowly on the iPad (for some reason much faster on the iPhone):
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self.navigationController setNavigationBarHidden:YES animated:NO];
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:frontgroundView];
CGContextBeginPath(context);
CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
CGContextStrokePath(context);
maskRef = CGBitmapContextCreateImage(context);
CGImageRef masked = CGImageCreateWithMask([backgroundImage CGImage], maskRef);
CGImageRelease(maskRef);
CGContextRef drawContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(drawContext);
CGContextTranslateCTM (drawContext,0,frontgroundView.frame.size.height);
CGContextScaleCTM (drawContext, 1,-1);
CGContextDrawImage(drawContext, CGRectMake(0, 0, frontgroundView.frame.size.width, frontgroundView.frame.size.height), [frontgroundView.image CGImage]);
CGContextRestoreGState (drawContext);
CGContextDrawImage(drawContext, CGRectMake(0, 0, frontgroundView.frame.size.width, frontgroundView.frame.size.height), masked);
CGImageRef finalImage = CGBitmapContextCreateImage(drawContext);
frontgroundView.image = [UIImage imageWithCGImage:finalImage];
CGImageRelease(finalImage);
CGImageRelease(masked);
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 10)
mouseMoved = 0;
}
Any ideas??
You should do your drawing in -drawRect: for your view instead of directly in response to a touch.