Monday, 28 September 2015

Working With CoreGraphics and Implementation in Ios/iphone

=> First Use Take Enumration: Of Diffrent Drowing Item:


#import <UIKit/UIKit.h>

typedef enum {
    DrawingLine,
    DrawingPath = 1,
    DrawingFillPath = 2,
    DrawingRect = 3,
    DrawingFillRect = 4,
    DrawingCircle = 5,
    DrawingArc = 6,
    DrawingCubicCurve = 7,
    DrawingQuadraticCurve = 8,
    DrawingDashLine = 9,
    DrawingShadow = 10,
    DrawingImage = 11,
}DrawingType;

==> Here Inherited From UIView :

@interface CoreDrowingView : UIView

@property (nonatomic,assign)DrawingType type;

- (id)initWithFrame:(CGRect)frame anddrawingType:(DrawingType)aType;

@end

-----------------


=> Now to drow diffrent  thing Using Core Graphics like arc, parth ,circle, etc
#import "CoreDrowingView.h"

@implementation CoreDrowingView

- (id)initWithFrame:(CGRect)frame anddrawingType:(DrawingType)aType
{
    self = [super initWithFrame:frame];
    if (self) {
        self.type = aType;
        self.backgroundColor= [UIColor whiteColor];
        // Initialization code
    }
    return self;
}




// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
   
    //Drawing a Line
   
    switch (self.type) {
        case DrawingLine:{
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSetLineWidth(context, 2.0);
            CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
            CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
            CGColorRef color = CGColorCreate(colorspace, components);
            CGContextSetStrokeColorWithColor(context, color);
            CGContextMoveToPoint(context, 30, 30);
            CGContextAddLineToPoint(context, 300, 400);
            CGContextStrokePath(context);
            CGColorSpaceRelease(colorspace);
            CGColorRelease(color);
            break;
        }
           
          ==> Here Drowing  For Path Drawing:

        case DrawingPath:{
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSetLineWidth(context, 2.0);
            CGContextSetStrokeColorWithColor(context,
                                             [UIColor blueColor].CGColor);
            CGContextMoveToPoint(context, 100, 100);
            CGContextAddLineToPoint(context, 150, 150);
            CGContextAddLineToPoint(context, 100, 200);
            CGContextAddLineToPoint(context, 50, 150);
            CGContextAddLineToPoint(context, 100, 100);
            CGContextStrokePath(context);
            break;
        }
           
        ==> Here Drowing  For Rect Drawing:

        case DrawingRect:{
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSetLineWidth(context, 4.0);
            CGContextSetStrokeColorWithColor(context,
                                             [UIColor blueColor].CGColor);
            CGRect rectangle = CGRectMake(60,170,200,80);
            CGContextAddRect(context, rectangle);
            CGContextStrokePath(context);
            break;
        }
           
      ==> Here Drowing  ForFor Path With Fill Color:\

        case DrawingFillPath:{
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextMoveToPoint(context, 100, 100);
            CGContextAddLineToPoint(context, 150, 150);
            CGContextAddLineToPoint(context, 100, 200);
            CGContextAddLineToPoint(context, 50, 150);
            CGContextAddLineToPoint(context, 100, 100);
            CGContextSetFillColorWithColor(context,
                                           [UIColor redColor].CGColor);
            CGContextFillPath(context);
            break;
        }
           
         ==> Here Drowing  For Rect With Fill Color:

        case DrawingFillRect:{
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSetLineWidth(context, 4.0);
            CGContextSetStrokeColorWithColor(context,
                                             [UIColor blueColor].CGColor);
            CGRect rectangle = CGRectMake(60,170,200,80);
            CGContextAddRect(context, rectangle);
            CGContextStrokePath(context);
            CGContextSetFillColorWithColor(context,
                                           [UIColor redColor].CGColor);
            CGContextFillRect(context, rectangle);
            break;
        }
           
          ==> Here Drowing For Arc:-

        case DrawingArc:{
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSetLineWidth(context, 4.0);
            CGContextSetStrokeColorWithColor(context,
                                             [UIColor blueColor].CGColor);
            CGContextMoveToPoint(context, 100, 100);
            CGContextAddArcToPoint(context, 100,200, 300,200, 100);
            CGContextStrokePath(context);
            break;
        }
           
          ==> Here Drowing For Circle:-

        case DrawingCircle:{
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSetLineWidth(context, 4.0);
            CGContextSetStrokeColorWithColor(context,
                                             [UIColor blueColor].CGColor);
            CGRect rectangle = CGRectMake(60,170,200,80);
            CGContextAddEllipseInRect(context, rectangle);
            CGContextStrokePath(context);
            break;
        }
           
          ==> Here Drowing  For Cubic Curve:-


        case DrawingCubicCurve:{
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSetLineWidth(context, 4.0);
            CGContextSetStrokeColorWithColor(context,
                                             [UIColor blueColor].CGColor);
            CGContextMoveToPoint(context, 10, 10);
            CGContextAddCurveToPoint(context, 0, 50, 300, 250, 300, 400);
            CGContextStrokePath(context);
            break;
        }
           
            ==> Here Drowing  For Quadratic Curve:-

        case DrawingQuadraticCurve:{
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSetLineWidth(context, 4.0);
            CGContextSetStrokeColorWithColor(context,
                                             [UIColor blueColor].CGColor);
            CGContextMoveToPoint(context, 10, 200);
            CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200);
            CGContextStrokePath(context);
            break;
        }
           
            ==> Here Drowing  For  Dash Line:-

        case DrawingDashLine:{
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSetLineWidth(context, 20.0);
            CGContextSetStrokeColorWithColor(context,
                                             [UIColor blueColor].CGColor);
            CGFloat dashArray[] = {2,6,4,2};
            CGContextSetLineDash(context, 3, dashArray, 4);
            CGContextMoveToPoint(context, 10, 200);
            CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200);
            CGContextStrokePath(context);
            break;
        }
           
                    ==> Here Drowing  For Draw Shadow On Any Object:

        case DrawingShadow:{
            CGContextRef context = UIGraphicsGetCurrentContext();
           
            CGSize  myShadowOffset = CGSizeMake (-10,  15);
           
            CGContextSaveGState(context);
           
            CGContextSetShadow (context, myShadowOffset, 5);
           
            CGContextSetLineWidth(context, 4.0);
            CGContextSetStrokeColorWithColor(context,
                                             [UIColor blueColor].CGColor);
            CGRect rectangle = CGRectMake(60,170,200,80);
            CGContextAddEllipseInRect(context, rectangle);
            CGContextStrokePath(context);
            CGContextRestoreGState(context);
            break;
        }

          ==> Here Drowing   Draw CGImage:-

        case DrawingImage:{
            UIImage *myImage = [UIImage imageNamed:@"smiley-small.png"];
//            CGPoint imagePoint = CGPointMake(0, 0);
//            [myImage drawAtPoint:imagePoint];
//            CGRect imageRect =CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
//            [myImage drawInRect:imageRect];
            CGRect imageRect =CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
            [myImage drawInRect:imageRect blendMode:kCGBlendModeNormal alpha:1.0];
           
            //kCGBlendModeColor
            //Uses the luminance values of the background with the hue and saturation values of the source image. This mode preserves the gray levels in the image. You can use this mode to color monochrome images or to tint color images.
           
            break;
        }

           
        default:
            break;
    }
}


@end