How to Smooth the drawing Stroke?

Posted by user1852420 on Stack Overflow See other posts from Stack Overflow or by user1852420
Published on 2012-11-27T05:01:33Z Indexed on 2012/11/27 5:04 UTC
Read the original article Hit count: 191

Filed under:
|

I am creating drawing.. i can undo, and put colors on it. but when i draw using my fingers the stroke is not that smooth and has edge lines,,

here my codes. on which I can Paint on a view, Undo, change color, and the opacity.

stroke.h

#import <UIKit/UIKit.h>

@interface stroke : UIView{
    NSMutableArray *strokeArray;
    UIColor *strokeColor;
    int strokeSize;
    float strokeAlpha;
    int strokeAlpha2;
    IBOutlet UISlider *slides;
    float red;
    float green;
    float blue;
    CGPoint mid1;
    CGPoint mid2; 
    CGPoint endingPoint,previousPoint1,previousPoint2;
    CGPoint currentTouch;
}

@property (nonatomic, retain) UIColor *strokeColor;
@property (nonatomic) int strokeSize;
@property (nonatomic, retain) NSMutableArray *strokeArray;
- (IBAction)changeAlphaValue;
-(void)loadSLider;
-(void)blueColor;
-(void)darkvioletColor;
-(void)violetColor;
-(void)pinkColor;
-(void)darkbrownColor;
-(void)redColor;
-(void)magentaRedColor;
-(void)lightBrownColor;
-(void)lightOrangeColor;
-(void)OrangeColor;
-(void)YellowColor;
-(void)greenColor;
-(void)lightYellowColor;
-(void)darkGreenColor;
-(void)TurquioseColor;
-(void)PaleTurquioseColor;
-(void)skyBlueColor;
-(void)whiteColor;
-(void)DirtyWhiteColor;
-(void)SilverColor;
-(void)LightGrayColor;
-(void)GrayColor;
-(void)LightBlackColor;
-(void)BlackColor;
@end

stroke.m

#import "stroke.h"

@implementation stroke
@synthesize strokeColor;
@synthesize strokeSize;
@synthesize strokeArray;


- (void) awakeFromNib{
    self.strokeArray = [[NSMutableArray alloc] init];
    self.strokeColor = [UIColor colorWithRed:0 green:0 blue:232 alpha:1];
    self.strokeSize = 3;
}
- (void)drawRect:(CGRect)rect{
    NSMutableArray *stroke;
    for (stroke in strokeArray) {
        CGContextRef contextRef = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(contextRef, [[stroke objectAtIndex:1] intValue]);
        CGFloat *color = CGColorGetComponents([[stroke objectAtIndex:2] CGColor]);
        CGContextSetRGBStrokeColor(contextRef, color[0], color[1], color[2], color[3]);    
        CGContextBeginPath(contextRef);
        CGPoint points[[stroke count]];
        for (NSUInteger i = 3; i < [stroke count]; i++) {
            points[i-3] = [[stroke objectAtIndex:i] CGPointValue];
        }
        CGContextAddLines(contextRef, points, [stroke count]-3);
        CGContextStrokePath(contextRef);
    }

}
-(void)loadSLider{

}
- (IBAction)changeAlphaValue{
    strokeAlpha2 =((int)slides.value);
}
-(void)blueColor{
    red = 0/255.0;
    green = 0/255.0;
    blue = 255/255.0;
}
-(void)darkvioletColor{
    red = 75/255.0;
    green = 0/255.0;
    blue = 130/255.0;
}
-(void)violetColor{
    red = 128/255.0;
    green = 0/255.0;
    blue = 128/255.0;
}
-(void)pinkColor{
    red = 255/255.0;
    green = 0/255.0;
    blue = 255/255.0;
}
-(void)darkbrownColor{
    red = 0.200;
    green = 0.0;
    blue = 0.0;
}
-(void)redColor{
    red = 255/255.0;
    green = 0/255.0;
    blue = 0/255.0;
}
-(void)magentaRedColor{
    red = 0.350;
    green = 0.0;
    blue = 0.0;
}
-(void)lightBrownColor{
    red = 0.480;
    green = 0.0;
    blue = 0.0;
}
-(void)lightOrangeColor{
    red = 0.600;
    green = 0.200;
    blue = 0.0;
}
-(void)OrangeColor{
    red = 1.0;
    green = 0.300;
    blue = 0.0;
}
-(void)YellowColor{
    red = 0.950;
    green = 0.450;
    blue = 0.0;
}
-(void)greenColor{
    red = 0.0;
    green = 1.0;
    blue = 0.0;
}
-(void)lightYellowColor{
    red = 1.0;
    green = 1.0;
    blue = 0.0;

}
-(void)darkGreenColor{
    red = 0.0;
    green = 0.500;
    blue = 0.0;
}
-(void)TurquioseColor{
    red = 0.0;
    green = 0.700;
    blue = 0.200;
}
-(void)PaleTurquioseColor{
    red = 0.0;
    green = 0.700;
    blue = 0.600;
}
-(void)skyBlueColor{
    red = 0.0;
    green = 0.400;
    blue = 0.800;
}
-(void)whiteColor{
    red = 1.0;
    green = 1.0;
    blue = 1.0;
}
-(void)DirtyWhiteColor{
    red = 0.800;
    green = 0.800;
    blue = 0.800;
}
-(void)SilverColor{
    red = 0.600;
    green = 0.600;
    blue = 0.600;
}
-(void)LightGrayColor{
    red = 0.500;
    green = 0.500;
    blue = 0.500;
}
-(void)GrayColor{
    red = 0.300;
    green = 0.300;
    blue = 0.300;
}
-(void)LightBlackColor{
    red = 0.150;
    green = 0.150;
    blue = 0.150;
}
-(void)BlackColor{
    red = 0.0;
    green = 0.0;
    blue = 0.0;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch;

    NSEnumerator *counter = [touches objectEnumerator];
    while ((touch = (UITouch *)[counter nextObject])) {
        switch (strokeAlpha2) {
            case 1:
                strokeAlpha = .1;
                break;
            case 2:
                strokeAlpha = .2;
                break;
            case 3:
                strokeAlpha = .3;
                break;
            case 4:
                strokeAlpha = .4;
                break;
            case 5:
                strokeAlpha = .5;
                break;
            case 6:
                strokeAlpha = .6;
                break;
            case 7:
                strokeAlpha = .7;
                break;
            case 8:
                strokeAlpha = .8;
                break;
            case 9:
                strokeAlpha = .9;
                break;
            case 10:
                strokeAlpha = 1;
                break;
            default:
                strokeAlpha = 1;
                break;
        }

        self.strokeColor = [UIColor colorWithRed:red green:green blue:blue alpha:strokeAlpha];
        NSValue *touchPos = [NSValue valueWithCGPoint:[touch locationInView:self]];
        UIColor *color = [UIColor colorWithCGColor:strokeColor.CGColor];
        NSNumber *size = [NSNumber numberWithInt:strokeSize]; 
        NSMutableArray *stroke = [NSMutableArray arrayWithObjects: touch, size, color, touchPos, nil];
        [strokeArray addObject:stroke];
    }
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch;

    NSEnumerator *counter = [touches objectEnumerator];

    while ((touch = (UITouch *)[counter nextObject])) {
        NSMutableArray *stroke;
        for (stroke in strokeArray) {
            if ([stroke objectAtIndex:0] == touch) {
                [stroke addObject:[NSValue valueWithCGPoint:[touch locationInView:self]]];
            }
            [self setNeedsDisplay];
        }
    }
}



@end

© Stack Overflow or respective owner

Related posts about xcode

Related posts about painting