Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Brush Hardness like Photoshop

How to get the following brush smoothness(hardness) effect like photoshop?

My attempt:

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSaveGState(context);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, 30);
CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:0.5f].CGColor);
CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 20.0f, [UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0f].CGColor);
CGContextAddPath(context, path);
CGContextStrokePath(context);
CGContextRestoreGState(context);

I tried adjusting alpha values, and shadow blur factor, but no successful result.

Does anybody have a solution to this? Any help would be appreciated.

like image 245
Fury Avatar asked Aug 14 '12 04:08

Fury


People also ask

What is the hardness of a brush in Photoshop?

The hardness setting is available only for Photoshop's round brushes. Setting the hardness as a percentage from 0 to 100 will feather the edges of the brush; 100% is a crisp edge, and 0% is a completely feathered edge from the center of the brush to the circumference.

What is hardness Photoshop?

Hardness: If you want to make the edges of the brush harder or softer, you can adjust the hardness from the same drop-down menu. A harder brush will have clear, defined edges, whereas a softer brush will have blurry, less-defined edges.

Why can't I change brush hardness in Photoshop?

If the brush hardness is greyed out and not editable in Photoshop, it is because you are using a brush preset that isn't compatible with hardness. For example, the pencil brush preset does not allow for the hardness to be adjusted. To make the hardness editable, you must select a different brush.

How do you create a brush in Photoshop for iPad?

Create your own brushes using the Adobe Capture app. Use Adobe Capture to create any brush file using your camera — make your own strokes on paper or take any photo or object and turn them into a custom brush. You can then save your brush to an . abr or send it directly to Photoshop on iPad.


2 Answers

On this image you can see following code result. I believe it is almost same to what you want.

enter image description here

Just outer shadow is not just enough to give that smooth effect that is why I add some inner shadow to shape with white color.

- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();

    // Shadows
    UIColor* shadow = UIColor.redColor;
    CGSize shadowOffset = CGSizeMake(0.1, -0.1);
    CGFloat shadowBlurRadius = 11;
    UIColor* shadow2 = UIColor.whiteColor; // Here you can adjust softness of inner shadow.
    CGSize shadow2Offset = CGSizeMake(0.1, -0.1);
    CGFloat shadow2BlurRadius = 9;

    // Rectangle Drawing
    UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(59, 58, 439, 52) cornerRadius: 21];
    CGContextSaveGState(context);
    CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius, [shadow CGColor]);
    [UIColor.redColor setFill];
    [rectanglePath fill];

    // Rectangle Inner Shadow
    CGContextSaveGState(context);
    UIRectClip(rectanglePath.bounds);
    CGContextSetShadowWithColor(context, CGSizeZero, 0, NULL);

    CGContextSetAlpha(context, CGColorGetAlpha([shadow2 CGColor]));
    CGContextBeginTransparencyLayer(context, NULL);
    {
        UIColor* opaqueShadow = [shadow2 colorWithAlphaComponent: 1];
        CGContextSetShadowWithColor(context, shadow2Offset, shadow2BlurRadius, [opaqueShadow CGColor]);
        CGContextSetBlendMode(context, kCGBlendModeSourceOut);
        CGContextBeginTransparencyLayer(context, NULL);

        [opaqueShadow setFill];
        [rectanglePath fill];

        CGContextEndTransparencyLayer(context);
    }
    CGContextEndTransparencyLayer(context);
    CGContextRestoreGState(context);

    CGContextRestoreGState(context);
}

Regarding size of the shape you have to adjust both inner and outer shadows blur radius.

like image 200
modus Avatar answered Sep 23 '22 20:09

modus


You can get an effect similar to what you're trying to achieve by blending your shadow with your stroke

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddPath(context, path);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextSetShadowWithColor(context, CGSizeMake(0.f, 0.f), self.lineWidth/4, [self.lineColor CGColor]);
CGContextSetBlendMode(context, kCGBlendModeMultiply);
CGContextSetAlpha(context, self.lineAlpha);
CGContextStrokePath(context);

With Multiply blending mode, using white color as stroke color and setting the color of the brush you want to the shadow, you get the following result:

enter image description here

I've connected the drawing function to touchesMoved event, so that way the longer I take to paint a part of the image, the harder the "Brush" draws (see the black line).

like image 23
Efesus Avatar answered Sep 23 '22 20:09

Efesus