CoreGraphics optimization - iphone

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.

Related

Drawing the lines in iphone app using touches event

I am drawing the signatures using the touch event i have got the code from forum it works fine but problem is that it does not draw line correct like if we draw line as ali the it collapse ali in the line here is my code
here is the screen of my drawing
- (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();
}
}
You can add button on pressing of that button a arrow is add on the screen and you can rotate and resize using touch.

How to unerase an erased UIImage?

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.

How to draw a line with exact x and y positions?

I need to work on a drawing application by using default delegate methods touches begin and touches moved.
But when I draw a line at any point of the screen, their x and y positions are changed automatically.
Actual problem in my application is: I draw a line at one position but that line is displayed in another place. What do I have to do to solve this problem?
My code is:
drawImage = [[UIImageView alloc] initWithImage:nil];
///drawImage.frame=CGRectMake(0,0, 320, 380);
drawImage.frame = self.view.frame;
[self.view addSubview:drawImage];
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touches moved method");
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)];
///[drawImage.image drawInRect:CGRectMake(0, 0, 320, 380)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), components[0],components[1],components[2],alpha);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
///mouseMoved++;
mouseMoved++;
if (mouseMoved == 10)
{
NSLog(#"if mouse moved is equal to 10");
mouseMoved = 0;
}
NSLog(#"touches moved method ended");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touches Ended method starting");
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2)
{
NSLog(#"touches tap count==2");
///drawImage.image = nil;
return;
}
if(!mouseSwiped)
{
NSLog(#"not equla mouse swiped");
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
///[drawImage.image drawInRect:CGRectMake(0, 0, 320, 380)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
if (tagindex==1)
{
NSLog(#"1111");
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
}
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
Thanks in Advance
try to substitute
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
with
UIGraphicsBeginImageContext(drawImage.frame.size);
[self.firma.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
and if u don't have the status bar, remove: currentPoint.y -= 20;
hope this is the problem.

Drawing and hiding the toolbar

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?

Want to add manual erasing option in ipad painting application by Quartz

I am working on a painting application using Quartz 2D for ipad. Now I want to add an eraser option so that user can manually erase portion of his drawn line with touch.I have also background image. Can anyone please help?
yes this is work well in my app ;-)
firstly you add this code in touch began
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:imgview];
UIGraphicsBeginImageContext(imgview.frame.size);
[imgview.image drawInRect:CGRectMake(0, 0, imgview.frame.size.width, imgview.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), lineWidth);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:img].CGColor);
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [UIColor clearColor].CGColor);
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
CGContextBeginPath(UIGraphicsGetCurrentContext());
here img is ur background image then on move touch you simple write the line stroke code that code is this
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:imgview];
NSLog(#"asdasdas");
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
imgview.image = UIGraphicsGetImageFromCurrentImageContext();
lastPoint = currentPoint;
then you see the final result is produce ;-)
if(erase)
{
UITouch *touch = [touches anyObject];
CGPoint currentTouch = [touch locationInView:extraImageVw];
CGFloat brushSize;
if (isEraser)
{
brushSize=25.0;
}
else
{
brushSize=25.0;
}
CGColorRef strokeColor = [UIColor whiteColor].CGColor;
UIGraphicsBeginImageContext(extraImageVw.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[extraImageVw.image drawInRect:CGRectMake(0, 0, extraImageVw.frame.size.width, extraImageVw.frame.size.height)];
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, brushSize);
if (isEraser) {
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:fullPathToFile]].CGColor);
}
else
{
CGContextSetStrokeColorWithColor(context, strokeColor);
CGContextSetBlendMode(context, kCGBlendModeClear);
}
CGContextSaveGState(UIGraphicsGetCurrentContext());
CGContextBeginPath(context);
CGContextMoveToPoint(context, Lastpoint.x, Lastpoint.y);
CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
CGContextStrokePath(context);
extraImageVw.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Lastpoint = [touch locationInView:extraImageVw];
}