Wednesday, June 5, 2013

UIBezierPath strokeWithBlendMode example in Objective C (iOS).


UIBezierPath strokeWithBlendMode

Draws a line along the receiver’s path using the specified blend mode and transparency values.

- (void)strokeWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha

Parameters of [UIBezierPath strokeWithBlendMode]
blendMode
The blend mode determines how the stroked path is composited with any existing rendered content.
alpha
The amount of transparency to apply to the stroked path. Values can range between 0.0 (transparent) and 1.0 (opaque). Values outside this range are clamped to 0.0 or 1.0.

Discussion of [UIBezierPath strokeWithBlendMode]
The drawn line is centered on the path with its sides parallel to the path segment. This method applies the current stroke color and drawing properties (plus the specified blend mode and transparency value) to the rendered path.

This method automatically saves the current graphics state prior to drawing and restores that state when it is done, so you do not have to save the graphics state yourself.

UIBezierPath strokeWithBlendMode example.
int count = 0;
for(UIBezierpath *_paths in pathArray)
{
   UIColor *_color = [delegate1.colorArray objectAtIndex:q];
   [_color setStroke];
   [_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
   count++;
}

Example of [UIBezierPath strokeWithBlendMode].
 - (void)drawRect:(CGRect)rect
    {
        [strokeColor setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)
        for(UIBezierPath *_path in pathArray)
            [myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
    }

    #pragma mark - Touch Methods
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        myPath=[[UIBezierPath alloc]init];
        myPath.lineWidth = currentSliderValue;

        UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
        [myPath moveToPoint:[mytouch locationInView:self]];
        [pathArray addObject:myPath];
    }
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
        [myPath addLineToPoint:[mytouch locationInView:self]];
        [self setNeedsDisplay];

    }

UIBezierPath strokeWithBlendMode example.
 - (void)drawBitmap
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
    [strokeColor setStroke];
    if (!incrementalImage) // first time draw; paint background white by ...
    {
        UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object
        [[UIColor colorWithPatternImage:[UIImage imageNamed:@"two.jpg"]] setFill];
        //[[UIColor whiteColor] setFill];
        [rectpath fill]; // filling it with white
        NSLog(@"========== ... drawBitmap .. CALLED=======");
    }
    [incrementalImage drawAtPoint:CGPointZero];

    for(NSDictionary *_pathDict in pathArray)
    {
        [((UIColor *)[_pathDict valueForKey:@"color"]) setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)
        [[_pathDict valueForKey:@"path"] strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
    }
    incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    }

End of UIBezierPath strokeWithBlendMode example article.