Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 14, 2010, 11:05 AM   #1
m1anoj
macrumors newbie
 
Join Date: Oct 2009
Issue with erase or transparent stroke using CoreGraphics framework

Hi Friends,
In one of my apps, am trying to erase/transparent stroke a part of UIImage which am drawing using CoreGraphics framework (CGContextRef etc..). Well in the process am able to clear the drawing in one shot by calling "removeAllObjects" message. but I could not able to figure it out, how to erase a part of the drawing image. Gosh!! i sat the whole day but still no results, nw its killing me . Please guys help me out from here. In short, my requirement is something like an eraser which can erase a part of of my drawing image. Appreciate ur help!!
m1anoj is offline   0 Reply With Quote
Old Jul 14, 2010, 11:23 AM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Can you post your code? removeAllObjects makes no sense in this context. If you are trying to "paint" with transparent then you are probably in the wrong drawing mode: it's painting transparent over the top of the current image in the buffer which does not alter it. You need to set the correct blend mode with CGContextSetBlendMode. If I remember correctly then kCGBlendModeClear allows you to use a clear colour to erase back to transparent.
robbieduncan is offline   0 Reply With Quote
Old Jul 14, 2010, 01:18 PM   #3
m1anoj
Thread Starter
macrumors newbie
 
Join Date: Oct 2009
Quote:
Originally Posted by robbieduncan View Post
Can you post your code? removeAllObjects makes no sense in this context. If you are trying to "paint" with transparent then you are probably in the wrong drawing mode: it's painting transparent over the top of the current image in the buffer which does not alter it. You need to set the correct blend mode with CGContextSetBlendMode. If I remember correctly then kCGBlendModeClear allows you to use a clear colour to erase back to transparent.
well i do nt ve the source code now, i can post it tomorrow.But i had tried with CGContextSetBlendMode as well, may b am going on a direction, i just could not figure out with that. removeAllObjects is been called as am using a NSMutableArray to keep the drawing objects in that buffer, which am clearing by calling the removeAllObjects message. Here i am not trying to "paint" with transparent, but i dont know where & how to use CGContextSetBlendMode. My app has three tab bar items, one to save the drawing image, 2nd one is to clear the image & 3rd one is to clear/erase a portion of the drawing image (something like eraser in MSPaint) which i just posted here as my query.
Please help me on this as am still not very good @ CoreGraphics/2D physics.
I ll post the code for your reference tomorrow.
m1anoj is offline   0 Reply With Quote
Old Jul 14, 2010, 01:58 PM   #4
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Before we go any further you will need to sort out your UI: you should not be using a tab bar as a toolbar. Follow the HIG and use the correct controls for the purpose.
robbieduncan is offline   0 Reply With Quote
Old Jul 14, 2010, 02:37 PM   #5
m1anoj
Thread Starter
macrumors newbie
 
Join Date: Oct 2009
Quote:
Originally Posted by robbieduncan View Post
Before we go any further you will need to sort out your UI: you should not be using a tab bar as a toolbar. Follow the HIG and use the correct controls for the purpose.
Oops, my mistake, i wrongly posted here tab bar, its actually a toolbar with 3 items to save image,clear image & erase some portion of image.
m1anoj is offline   0 Reply With Quote
Old Jul 14, 2010, 02:51 PM   #6
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by m1anoj View Post
Oops, my mistake, i wrongly posted here tab bar, its actually a toolbar with 3 items to save image,clear image & erase some portion of image.
OK, good. Then lets wait for the code and see where we go...
robbieduncan is offline   0 Reply With Quote
Old Jul 15, 2010, 02:56 AM   #7
m1anoj
Thread Starter
macrumors newbie
 
Join Date: Oct 2009
Quote:
Originally Posted by robbieduncan View Post
OK, good. Then lets wait for the code and see where we go...
Ok here is my code:

DrawView.h:

#import <UIKit/UIKit.h>


@interface DrawView : UIView {

UIImage *myPic;
NSMutableArray *myDrawing;
}

-(void)drawPicUIImage *)thisPic;
-(void)erasePic;
-(void)eraseStart;
-(void)eraseEnd;
-(void)cancelDrawing;

@end

DrawView.m:

#import "DrawView.h"


@implementation DrawView

-(id)init{


return self;
}

-(void)drawPicUIImage *)thisPic {

myPic = thisPic;
[myPic retain];
[self setNeedsDisplay];
}

- (void)drawRectCGRect)rect {

float newHeight;
float newWidth;

if (!myDrawing) {
myDrawing = [[NSMutableArray alloc] initWithCapacity:0];
}

[[UIColor redColor] set];
CGContextRef ctx = UIGraphicsGetCurrentContext();

if (myPic != NULL) {
float ratio = myPic.size.height/460;
if (myPic.size.width/320 > ratio) {
ratio = myPic.size.width/320;
}

newHeight = myPic.size.height/ratio;
newWidth = myPic.size.width/ratio;

[myPic drawInRect:CGRectMake(0,0,newWidth,newHeight)];
}
if ([myDrawing count] > 0) {
CGContextSetLineWidth(ctx, 5);

for (int i = 0 ; i < [myDrawing count] ; i++) {
NSArray *thisArray = [myDrawing objectAtIndex:i];

if ([thisArray count] > 2) {
float thisX = [[thisArray objectAtIndex:0] floatValue];
float thisY = [[thisArray objectAtIndex:1] floatValue];

CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, thisX, thisY);

for (int j = 2; j < [thisArray count] ; j+=2) {
thisX = [[thisArray objectAtIndex:j] floatValue];
thisY = [[thisArray objectAtIndex:j+1] floatValue];

CGContextAddLineToPoint(ctx, thisX,thisY);
}
CGContextStrokePath(ctx);
}
}
}
}

- (void) touchesBeganNSSet *)touches withEventUIEvent *)event {

[myDrawing addObject:[[NSMutableArray alloc] initWithCapacity:4]];

CGPoint curPoint = [[touches anyObject] locationInView:self];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];
}

- (void) touchesMovedNSSet *)touches withEventUIEvent *)event {

CGPoint curPoint = [[touches anyObject] locationInView:self];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];

[self setNeedsDisplay];
}

- (void) touchesEndedNSSet *)touches withEventUIEvent *)event {

CGPoint curPoint = [[touches anyObject] locationInView:self];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
[[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];

[self setNeedsDisplay];

}

-(void)eraseStart
{

}

-(void)eraseEnd
{

}

-(void)erasePic
{

}

-(void)cancelDrawing {

[myDrawing removeAllObjects];
[self setNeedsDisplay];

}

- (void)dealloc {
[super dealloc];
[myPic release];
[myDrawing release];
}


@end

Please let me know if anything else you want.
m1anoj is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
iPhone: Restoring iPhone 4s 5.1.1 - "erase all" after jail break issue vkuzin Jailbreaks and iOS Hacks 1 Jan 9, 2014 12:14 AM
Disc Utility issue - trying to erase hdd, but erase tab is greyed out? Whackintosh OS X 10.8 Mountain Lion 5 Apr 12, 2013 02:59 AM
Resolved: [Applescript] Security Framework issue dezzeus Mac Programming 1 Mar 12, 2013 02:37 PM
Pen stroke issue Hexiii Design and Graphics 4 Feb 7, 2013 03:56 PM
Erase and Sync issue Mark07 iPhone Tips, Help and Troubleshooting 3 Oct 19, 2012 03:53 PM

Forum Jump

All times are GMT -5. The time now is 07:16 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC