Issue with erase or transparent stroke using CoreGraphics framework

Discussion in 'iPhone/iPad Programming' started by m1anoj, Jul 14, 2010.

  1. macrumors newbie

    #1
    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!!
     
  2. Moderator

    robbieduncan

    Staff Member

    #2
    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.
     
  3. macrumors newbie

    #3
    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.
     
  4. Moderator

    robbieduncan

    Staff Member

    #4
    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.
     
  5. macrumors newbie

    #5
    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. :)
     
  6. Moderator

    robbieduncan

    Staff Member

    #6
    OK, good. Then lets wait for the code and see where we go...
     
  7. macrumors newbie

    #7
    Ok here is my code:

    DrawView.h:

    #import <UIKit/UIKit.h>


    @interface DrawView : UIView {

    UIImage *myPic;
    NSMutableArray *myDrawing;
    }

    -(void)drawPic:(UIImage *)thisPic;
    -(void)erasePic;
    -(void)eraseStart;
    -(void)eraseEnd;
    -(void)cancelDrawing;

    @end

    DrawView.m:

    #import "DrawView.h"


    @implementation DrawView

    -(id)init{


    return self;
    }

    -(void)drawPic:(UIImage *)thisPic {

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

    - (void)drawRect:(CGRect)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) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)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) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

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

    [self setNeedsDisplay];
    }

    - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)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.
     

Share This Page