DrawRect drawing function

1. Draw a line

*/ - (void)drawRect:(CGRect)rect {/** 1. For context * / CGContextRef context = UIGraphicsGetCurrentContext (); /** create path object */ CGMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path, nil, 20, 50); /** Move to the specified position (set path start) */ CGPathAddLineToPoint(path, nil, 20, 100); /** Draw a line (from the starting position) */ CGPathAddLineToPoint(path, nil, 300, 100); /** draw another line, starting at the end of the previous line */ /** 3. Add path to context */ CGContextAddPath(context, path); / * * 4. Set to implement context properties * / CGContextSetRGBStrokeColor (context, 1.0, 1.0, 1.0, 1); /** Set the stylus color */ CGContextSetRGBFillColor(context, 1.0, 0, 0, 1); /** Set the fill color */ CGContextSetLineWidth(context, 2.0); /** Set line width */ CGContextSetLineCap(context, kCGLineCapRound); /** set the vertex style, (20,50) and (300,100) are vertices */ CGContextSetLineJoin(context, kCGLineJoinRound); Lengths (e.g., the following definition indicates the lengths of the first lengths to 8, then lengths to 3 to redraw the lengths to 8, Of course this array can have more elements) count: dashed array elements */ CGFloat lengths[2] = {18, 9}; CGContextSetLineDash(context, 0, lengths, 2); /* Set the shadow context: blur: offset: shadow color */ CGColorRef color = [UIColor grayColor].CGColor; // Color conversion, because Quartz 2D is cross-platform, it cannot use UIKit objects. But UIkit provides CGContextSetShadowWithColor transformation method (context, CGSizeMake (2, 2), 0.8, color); */ /*CGPathDrawingMode is the fill mode, enumeration type kCGPathFill: fill only (non-zero winding number fill), no border is drawn kCGPathEOFill: odd-even rule fill (when multiple paths cross, odd number cross fill, KCGPathFillStroke: both border and fill kCGPathEOFillStroke: Parity fills and draws borders */ CGContextDrawPath(context, kCGPathFillStroke); // The last argument is the fill type /** 6. }Copy the code

Simplified line drawing

- (void) drawLine2 {/ / 1. Get the graphics context CGContextRef context = UIGraphicsGetCurrentContext (); CGContextMoveToPoint(context, 20, 50); CGContextAddLineToPoint(context, 20, 100); CGContextAddLineToPoint(context, 300, 100); A. Create a starting and ending line, not recommended //CGPathAddLineToPoint(path, nil, 20, 50); B. Call the path blocking method CGContextClosePath(context); //3. Set the graphics context properties [[UIColor redColor]setStroke]; // Set the red border [[UIColor greenColor]setFill]; //[[UIColor blueColor]set]; CGContextDrawPath(context, kCGPathFillStroke); }Copy the code

2. Draw the ellipse

#pragma mark - Draws ellipsesCGRect rect = CGRectMake(50, 50, 220.0, 200.0); -(void)drawEllipse:(CGContextRef)context {// add an object and draw an ellipse (circle). CGContextAddEllipseInRect(context, rect); // Set properties [[UIColor purpleColor]set]; // Draw CGContextDrawPath(context, kCGPathFillStroke); }Copy the code

3. Draw arcs

#pragma Mark Draws arcs-(void)drawArc:(CGContextRef)context{/* Add an arc object x: center x-coordinate Y: center y-coordinate RADIUS: radius startAngle: start radian endAngle: end radian Closewise: Whether to draw counterclockwise, 0 clockwise */ CGContextAddArc(context, 160, 160, 100.0, 0.0, M_PI_2, 1); // Set properties [[UIColor yellowColor]set]; // Draw CGContextDrawPath(context, kCGPathFillStroke); }Copy the code

4. Draw bezier curves

Pragma Mark draws Bessel curves-(void)drawCurve (CGContextRef)context {CGContextMoveToPoint(context, 20, 100); / / move to the starting position of quadratic bezier curve drawing / * c: graphics context CPX: control point x coordinate cpy: control point coordinates x, y end point x coordinate y: end points y * / CGContextAddQuadCurveToPoint (context, 160, 0, 300, 100); CGContextMoveToPoint(context, 20, 500); Cp1x: the first control point x cp1y: the first control point y cp2x: the second control point x cp2y: the second control point y x: the end point X y: the end point y */ CGContextAddCurveToPoint(context, 80, 300, 240, 500, 300, 300); // Set the graphics context properties [[UIColor yellowColor]setFill];
    [[UIColor redColor]setStroke]; // Draw path CGContextDrawPath(context, kCGPathFillStroke); }Copy the code

5. Draw text

-(void)drawText:(CGContextRef)context {// draw the content to a specified area NSString * STR =@"Star Walk is the most beautiful stargazing app you’ve ever seen on a mobile device. It will become your go-to interactive astro guide to the night sky, following your every movement in real-time and allowing you to explore over 200, 000 celestial bodies with extensive information about stars and constellations that you find."; CGRect rect= CGRectMake(20, 50, 280, 300); UIFont *font=[UIFont systemFontOfSize:18]; // set the font UIColor *color=[UIColor redColor]; NSMutableParagraphStyle *style=[[NSMutableParagraphStyle alloc]init]; NSTextAlignment align=NSTextAlignmentLeft; // Alignment style. Alignment = align; [str drawInRect:rect withAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:color,NSParagraphStyleAttributeName:style}]; }Copy the code

6. Image rendering

#pragma Mark - Image drawing
-(void)drawImage:(CGContextRef)context {
    UIImage *image=[UIImage imageNamed:@"image2.jpg"]; // Draw from a point [image drawAtPoint:CGPointMake(10, 50)]; //[image drawInRect:CGRectMake(10, 50, 300, 450)] [image drawInRect:CGRectMake(10, 50, 300, 450)] / / draws / / [image drawAsPatternInRect: CGRectMake (0, 0, 320, 568)]. }Copy the code

Draw a gradient view

#import "ShadeView.h"
#define TILE_SIZE 20
@implementation ShadeView

-(void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
//     [self drawLinearGradient:context];
//    [self drawRadialGradient:context];
//    [self drawBackgroundWithColoredPattern:context];
    [self drawImage2:context];

}

#pragma mark Linear gradient- (void) drawLinearGradient context: (CGContextRef) {/ / use the RGB color space CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB (); Note that since RGB color space is specified, four array elements represent one color (red, green, blue, alpha). Locations: Locations of colors (range 0 to 1). The number of colors in the array is no less than the number of colors stored in the components. Count: Number of gradients. = number of locations */ CGFloat compoents[12] = {248.0/255.0, 86.0/255.0, 86.0/255.0, 1, 249.0/255.0, 127.0/255.0, 127.0/255.0, 1, 1.0, 1.0, 1.0, 1.0}; CGFloat locations[3] = {0, 0.3, 1.0}; CGGradientRef gradient= CGGradientCreateWithColorComponents(colorSpace, compoents, locations, 3); / * draw linear gradient context: graphics context gradient: gradient the startPoint: starting position the endPoint: end location options: drawing modes, kCGGradientDrawsBeforeStartLocation Before beginning to paint, no longer draw after end position, kCGGradientDrawsAfterEndLocation before starting position not to paint, To the end point after continue filling * / CGContextDrawLinearGradient (context, gradient, CGPointZero, CGPointMake (320, 300), kCGGradientDrawsBeforeStartLocation); CGColorSpaceRelease(colorSpace); }Pragma mark Radial gradient- (void) drawRadialGradient context: (CGContextRef) {/ / use the RGB color space CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();  Note that since RGB color space is specified, four array elements represent one color (red, green, blue, alpha). Locations: Locations of colors (range 0 to 1). The number of colors in the array is no less than the number of colors stored in the components. Count: Number of gradients. Is equal to the number of locations * / CGFloat compoents [12] = {1, the 248.0/255.0, 86.0/255.0, 86.0/255.0, 249.0/255.0, 127.0/255.0, 127.0/255.0, 1, 1.0, 1.0, 1.0, 1.0}; ,0.3 CGFloat locations [3] = {0, 1.0}; CGGradientRef gradient= CGGradientCreateWithColorComponents(colorSpace, compoents, locations, 3); StartCenter: startRadius: startRadius (usually 0, otherwise there is no padding in this radius) endCenter: end position (usually the same as the start point, Otherwise there will be a deviation) endRadius: at the end of the radius (that is, the gradient of the diffusion length) options: drawing modes, kCGGradientDrawsBeforeStartLocation drawing before starting position, but to the end position after drawing, no longer Don't draw before kCGGradientDrawsAfterEndLocation starting position, But to the end point after continue filling * / CGContextDrawRadialGradient (context, gradient, CGPointMake (160, 284), a 0, CGPointMake (165, 289), 150, kCGGradientDrawsAfterEndLocation); CGColorSpaceRelease(colorSpace); }#pragma mark - Cut rectangular fill gradient-(void)drawRectWithLinearGradientFill:(CGContextRef)context { CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); //CGContextClipToRect(context, CGRectMake(20, 50, 280, 300)); // You can also use UIKit's corresponding method UIRectClip(CGRectMake(20, 50, 280, 300)); CGFloat compoents [12] = {1, the 248.0/255.0, 86.0/255.0, 86.0/255.0, 249.0/255.0, 127.0/255.0, 127.0/255.0, 1, 1.0, 1.0, 1.0, 1.0}; ,0.3 CGFloat locations [3] = {0, 1.0}; CGGradientRef gradient= CGGradientCreateWithColorComponents(colorSpace, compoents, locations, 3); CGContextDrawLinearGradient(context, gradient, CGPointMake(20, 50), CGPointMake(300, 300), kCGGradientDrawsAfterEndLocation); CGColorSpaceRelease(colorSpace); }#pragma Mark - Color overlay/** When using Quartz 2D to draw, the image drawn behind overwrites the previous one. By default, if the previous one is overwritten, you can't see what's behind, but sometimes this is not what we want, so we provide fill mode in Quartz 2D for developers to configure and adjust. Because there are so many fill mode categories, here is an example: * / - (void) drawRectByUIKitWithContext2: (CGContextRef) context {CGRect the rect = CGRectMake (0, 130.0, 320.0, 50.0); CGRect rect1= CGRectMake(0, 390.0, 320.0, 50.0) CGRect rect2=CGRectMake(20, 50.0, 10.0, 250.0); CGRect rect3=CGRectMake(40.0, 50.0, 10.0, 250.0); CGRect rect4=CGRectMake(60.0, 50.0, 10.0, 250.0); CGRect rect5=CGRectMake(80.0, 50.0, 10.0, 250.0); CGRect rect6=CGRectMake(100.0, 50.0, 10.0, 250.0); CGRect rect7=CGRectMake(120.0, 50.0, 10.0, 250.0); CGRect rect8=CGRectMake(140.0, 50.0, 10.0, 250.0); CGRect rect9=CGRectMake(160.0, 50.0, 10.0, 250.0); CGRect rect10=CGRectMake(180.0, 50.0, 10.0, 250.0); CGRect rect11=CGRectMake(200.0, 50.0, 10.0, 250.0); CGRect rect12=CGRectMake(220.0, 50.0, 10.0, 250.0); CGRect rect13=CGRectMake(240.0, 50.0, 10.0, 250.0); CGRect rect14=CGRectMake(260.0, 50.0, 10.0, 250.0); CGRect rect15=CGRectMake(280.0, 50.0, 10.0, 250.0); CGRect rect16=CGRectMake(30.0, 310.0, 10.0, 250.0); CGRect rect17=CGRectMake(50.0, 310.0, 10.0, 250.0); CGRect rect18=CGRectMake(70.0, 310.0, 10.0, 250.0); CGRect rect19=CGRectMake(90.0, 310.0, 10.0, 250.0); CGRect rect20=CGRectMake(110.0, 310.0, 10.0, 250.0); CGRect rect21=CGRectMake(130.0, 310.0, 10.0, 250.0); CGRect rect22=CGRectMake(150.0, 310.0, 10.0, 250.0); CGRect rect23=CGRectMake(170.0, 310.0, 10.0, 250.0); CGRect rect24=CGRectMake(190.0, 310.0, 10.0, 250.0); CGRect rect25=CGRectMake(210.0, 310.0, 10.0, 250.0); CGRect rect26=CGRectMake(230.0, 310.0, 10.0, 250.0); CGRect rect27=CGRectMake(250.0, 310.0, 10.0, 250.0); CGRect rect28=CGRectMake(270.0, 310.0, 10.0, 250.0); CGRect rect29=CGRectMake(290.0, 310.0, 10.0, 250.0); [[UIColor yellowColor]set]; UIRectFill(rect);
    [[UIColor greenColor]setFill]; UIRectFill(rect1);
    [[UIColor redColor]setFill];
    UIRectFillUsingBlendMode(rect2, kCGBlendModeClear);
    UIRectFillUsingBlendMode(rect3, kCGBlendModeColor);
    UIRectFillUsingBlendMode(rect4, kCGBlendModeColorBurn);
    UIRectFillUsingBlendMode(rect5, kCGBlendModeColorDodge);
    UIRectFillUsingBlendMode(rect6, kCGBlendModeCopy);
    UIRectFillUsingBlendMode(rect7, kCGBlendModeDarken);
    UIRectFillUsingBlendMode(rect8, kCGBlendModeDestinationAtop);
    UIRectFillUsingBlendMode(rect9, kCGBlendModeDestinationIn);
    UIRectFillUsingBlendMode(rect10, kCGBlendModeDestinationOut);
    UIRectFillUsingBlendMode(rect11, kCGBlendModeDestinationOver);
    UIRectFillUsingBlendMode(rect12, kCGBlendModeDifference);
    UIRectFillUsingBlendMode(rect13, kCGBlendModeExclusion);
    UIRectFillUsingBlendMode(rect14, kCGBlendModeHardLight);
    UIRectFillUsingBlendMode(rect15, kCGBlendModeHue);
    UIRectFillUsingBlendMode(rect16, kCGBlendModeLighten);
    UIRectFillUsingBlendMode(rect17, kCGBlendModeLuminosity);
    UIRectFillUsingBlendMode(rect18, kCGBlendModeMultiply);
    UIRectFillUsingBlendMode(rect19, kCGBlendModeNormal);
    UIRectFillUsingBlendMode(rect20, kCGBlendModeOverlay);
    UIRectFillUsingBlendMode(rect21, kCGBlendModePlusDarker);
    UIRectFillUsingBlendMode(rect22, kCGBlendModePlusLighter);
    UIRectFillUsingBlendMode(rect23, kCGBlendModeSaturation);
    UIRectFillUsingBlendMode(rect24, kCGBlendModeScreen);
    UIRectFillUsingBlendMode(rect25, kCGBlendModeSoftLight);
    UIRectFillUsingBlendMode(rect26, kCGBlendModeSourceAtop);
    UIRectFillUsingBlendMode(rect27, kCGBlendModeSourceIn);
    UIRectFillUsingBlendMode(rect28, kCGBlendModeSourceOut);
    UIRectFillUsingBlendMode(rect29, kCGBlendModeXOR);

}

#pragma Mark - Tile fill

#pragma Mark - Has color fill modeVoid drawColoredTile(void *info,CGContextRef context) { Set the fill color CGContextSetRGBFillColor(context, 254.0/255.0, 52.0/255.0, 90.0/255.0, 1); CGContextFillRect(context, CGRectMake(0, 0, TILE_SIZE, TILE_SIZE)); CGContextFillRect(context, CGRectMake(TILE_SIZE, TILE_SIZE, TILE_SIZE, TILE_SIZE)); } - (void) drawBackgroundWithColoredPattern context: (CGContextRef) {/ / / / device independent color space CGColorSpaceRef rgbSpace = CGColorSpaceCreateDeviceRGB(); / / mode fill color space, pay attention to color filling pattern, here pass NULL CGColorSpaceRef colorSpace = CGColorSpaceCreatePattern (NULL); / / sets the fill color color space to the color of the pattern fill space CGContextSetFillColorSpace (the context, the colorSpace); // Fill the pattern callback structure CGPatternCallbacks callback={0,&drawColoredTile,NULL}; /* Fill mode info:// The parameter passed to callback bounds: tile size matrix: deform xStep: tile spacing yStep: tile spacing tiling: tile placement method IsClored: Whether the tile drawn has a specified color (specify bit here for colored tiles)trueCallbacks: callback function */ CGPatternRef Pattern =CGPatternCreate(NULL, CGRectMake(0, 0, 2*TILE_SIZE, 2*TILE_SIZE), CGAffineTransformIdentity, 2 * TILE_SIZE TILE_SIZE + 5 + 5, 2 * and kCGPatternTilingNoDistortion,true, &callback); CGFloat alpha=1; // Note that the last parameter specifies the address of the transparency parameter for the colored tile and the array CGContextSetFillPattern(context, pattern, &alpha) corresponding to the current color space for the colorless tile; UIRectFill(CGRectMake(0, 0, 320, 568)); // CGColorSpaceRelease(rgbSpace); CGColorSpaceRelease(colorSpace); CGPatternRelease(pattern); }#pragma Mark - Colorless fill mode// Fill tile callback (must satisfy CGPatternCallbacks signature) void drawTile(void *info,CGContextRef Context) {CGContextFillRect(context, CGRectMake(0, 0, TILE_SIZE, TILE_SIZE)); CGContextFillRect(context, CGRectMake(TILE_SIZE, TILE_SIZE, TILE_SIZE, TILE_SIZE)); } - (void) drawBackgroundWithPattern context: (CGContextRef) {/ / device independent color space CGColorSpaceRef rgbSpace = CGColorSpaceCreateDeviceRGB(); / / mode fill color space CGColorSpaceRef colorSpace = CGColorSpaceCreatePattern (rgbSpace); / / sets the fill color color space to the color of the pattern fill space CGContextSetFillColorSpace (the context, the colorSpace); // Fill the pattern callback structure CGPatternCallbacks callback={0,&drawTile,NULL}; /* Fill mode info:// The parameter passed to callback bounds: tile size matrix: deform xStep: tile spacing yStep: tile spacing tiling: tile placement method IsClored: Whether the tile drawn has a specified color (for colorless tiles, specify bit herefalseCallbacks: callback function */ CGPatternRef Pattern = CGPatternCreate(NULL, CGRectMake(0, 0, 2*TILE_SIZE, 2*TILE_SIZE), CGAffineTransformIdentity, 2 * TILE_SIZE TILE_SIZE + 5 + 5, 2 * and kCGPatternTilingNoDistortion,false, &callback); CGFloat components [] = {254.0/255.0, 52.0/255.0, 90.0/255.0, 1.0}; // Note that the last parameter specifies the current color space color data CGContextSetFillPattern(context, pattern, components) for colorless fill mode; // CGContextSetStrokePattern(context, pattern, components); UIRectFill(CGRectMake(0, 0, 320, 568)); CGColorSpaceRelease(rgbSpace); CGColorSpaceRelease(colorSpace); CGPatternRelease(pattern); }#pragma mark Graphics context deformation-(void)drawImage:(CGContextRef)context {// save the initial state CGContextSaveGState(context); CGContextTranslateCTM(context, 100, 0); // Deform step 2: scale 0.8 CGContextScaleCTM(context, 0.8, 0.8); // Rotate CGContextRotateCTM(context, m_PI_4/4); UIImage *image=[UIImage imageNamed:@"Screen snapshot 2016-04-28 11.14.29 am"]; [image drawInRect:CGRectMake(0, 50, 240, 300)]; CGContextRestoreGState(context); }#pragma Mark - Built-in picture drawing
-(void)drawImage2:(CGContextRef)context {
    UIImage *image = [UIImage imageNamed:@"Screen snapshot 2016-04-28 11.14.29 am"]; Rect = CGRectMake(10, 50, 300, 450); CGContextDrawImage(context, rect, image.CGImage); }Copy the code

Three, the use of filters

#import "FilterCoreViewController.h"
#define CONSTROLPANEL_FONTSIZE 12@interface FilterCoreViewController () <UINavigationControllerDelegate,UIImagePickerControllerDelegate> { UIImagePickerController *_imagePickerController; // System photo selection controller UIImageView *_imageView; // CIContext *_context; //Core Image context CIImage *_image; // We want to edit the image CIImage *_outputImage; CIFilter *_colorControlsFilter; } @implementation FilterCoreViewController - (void)viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. [self initLayout]; }#pragma mark initializes the layout-(void)initLayout {// Initializes the image selector _imagePickerController=[[UIImagePickerController alloc]init]; _imagePickerController.delegate =self; _imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 64, 320, 502)]; _imageView.contentMode=UIViewContentModeScaleAspectFit; [self.view addSubview:_imageView]; / / navigation buttons above the self. The navigationItem. Title = @"Enhance";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Open" style:UIBarButtonItemStyleDone target:self action:@selector(openPhoto:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Save"style:UIBarButtonItemStyleDone target:self action:@selector(savePhoto:)]; UIView *controlView=[[UIView alloc]initWithFrame:CGRectMake(0, 450, 320, 118)]; / / controlView. Alpha = 0.2; / / controlView. BackgroundColor = [UIColor colorWithRed: 46.0/255.0 green: 178.0/255.0 blue: 235.0/255.0 alpha: 1); [self.view addSubview:controlView]; UILabel *lbSaturation=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 60, 25)]; lbSaturation.text=@"Saturation"; lbSaturation.font=[UIFont systemFontOfSize:CONSTROLPANEL_FONTSIZE]; [controlView addSubview:lbSaturation]; UISlider *sldStaturation=[[UISlider alloc]initWithFrame:CGRectMake(80, 10, 230, 30)]; / / note that although UISlider height can't adjust, a lot of friends will say height set bit 0 can, in fact after is set to 0 in iOS7 cannot drag [controlView addSubview: sldStaturation]; sldStaturation.minimumValue=0; sldStaturation.maximumValue=2; sldStaturation.value=1; [sldStaturation addTarget:self action:@selector(changeStaturation:)forControlEvents:UIControlEventValueChanged]; // Brightness (default: 0) UILabel *lbBrightness=[[UILabel alloc]initWithFrame:CGRectMake(10, 40, 60, 25)]; lbBrightness.text=@"Brightness";
    lbBrightness.font=[UIFont systemFontOfSize:CONSTROLPANEL_FONTSIZE];
    [controlView addSubview:lbBrightness];
    UISlider *sldBrightness=[[UISlider alloc]initWithFrame:CGRectMake(80, 40, 230, 30)];
    [controlView addSubview:sldBrightness];
    sldBrightness.minimumValue=-1;
    sldBrightness.maximumValue=1;
    sldBrightness.value=0;
    [sldBrightness addTarget:self action:@selector(changeBrightness:) forControlEvents:UIControlEventValueChanged]; // Contrast (default: 1) UILabel *lbContrast=[[UILabel alloc]initWithFrame:CGRectMake(10, 70, 60, 25)]; lbContrast.text=@"Contrast";
    lbContrast.font=[UIFont systemFontOfSize:CONSTROLPANEL_FONTSIZE];
    [controlView addSubview:lbContrast];
    UISlider *sldContrast=[[UISlider alloc]initWithFrame:CGRectMake(80, 70, 230, 30)];
    [controlView addSubview:sldContrast];
    sldContrast.minimumValue=0;
    sldContrast.maximumValue=2;
    sldContrast.value=1;
    [sldContrast addTarget:self action:@selector(changeContrast:) forControlEvents:UIControlEventValueChanged]; // create cpu-based image context // NSNumber *number=[NSNumber numberWithBool:YES]; // NSDictionary *option=[NSDictionary dictionaryWithObject:numberforKey:kCIContextUseSoftwareRenderer]; // _context=[CIContext contextWithOptions:option]; _context=[CIContext contextWithOptions:nil]; // Using GPU rendering is recommended, but note that the GPU CIContext is not accessible across applications. For example, calling context processing directly in UIImagePickerController's finish method will automatically degrade to CPU rendering, so it is recommended to save the image in the finish method now. Then in the main program calls / / EAGLContext * EAGLContext = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES1]; // _context=[CIContext contextWithEAGLContext:eaglContext]; _colorControlsFilter=[CIFilter filterWithName:@"CIColorControls"];
    
}

#pragma Mark opens the picture selector-(void)openPhoto:(UIBarButtonItem *) BTN {// Open image picker [self presentViewController:_imagePickerController animated:YES completion:nil]; }#pragma Mark Saves images- (void) savePhoto: (BTN UIBarButtonItem *) {/ / save the photos to the album UIImageWriteToSavedPhotosAlbum (_imageView. Image, nil nil, nil); UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Sytem Info" message:@"Save Success!" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert show];
}

Pragma mark selects the image proxy method-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ / / close the image selector [self dismissViewControllerAnimated: YES completion: nil]; / / the choice picture UIImage * selectedImage = [info objectForKey: UIImagePickerControllerOriginalImage]; _imageView.image = selectedImage; / / initialize CIImage source image _image = [CIImage imageWithCGImage: selectedImage. CGImage]; [_colorControlsFiltersetValue:_image forKey:@"inputImage"]; // Set the filter input image}#pragma Mark sets the output image to UIImageView
-(void)setImage { CIImage *outputImage = [_colorControlsFilter outputImage]; / / output image CGImageRef temp = [_context createCGImage: outputImage fromRect: [outputImage among]]. _imageView.image = [UIImage imageWithCGImage:temp]; CGImageRelease(temp); // Release the CGImage object}#pragma Mark Adjusts saturation
-(void)changeStaturation:(UISlider *)slider{
    [_colorControlsFilter setValue:[NSNumber numberWithFloat:slider.value] forKey:@"inputSaturation"]; // Set the filter parameter [self]setImage];
}

#pragma Mark Adjusts brightness
-(void)changeBrightness:(UISlider *)slider{
    [_colorControlsFilter setValue:[NSNumber numberWithFloat:slider.value] forKey:@"inputBrightness"];
    [self setImage];
}

#pragma Mark Adjusts contrast
-(void)changeContrast:(UISlider *)slider{
    [_colorControlsFilter setValue:[NSNumber numberWithFloat:slider.value] forKey:@"inputContrast"];
    [self setImage];
}
Copy the code