1、ios绘图/ ViewController.m/ 01-带有边框的图片裁剪/ Created by Gavin on 15/9/14./ Copyright (c) 2015年 Gavin. All rights reserved./#import ViewController.h#import UIImage+image.hinterface ViewController ()property (weak, nonatomic) IBOutlet UIImageView *imageV;endimplementation ViewController- (void)viewDidLoad s
2、uper viewDidLoad; /1.加载 UIImage *image = UIImage imageNamed:阿狸头像; /2.边框宽度 CGFloat borderW = 10; /3.开启图片上下文. CGSize size = CGSizeMake(image.size.width + 2 * borderW, image.size.height + 2 * borderW); UIGraphicsBeginImageContextWithOptions(size, NO, 0); /4.先描述一个大圆,设为填充 UIBezierPath *path = UIBezierPat
3、h bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height); UIColor redColor set; path fill; /5.再添加一个小圆,把小圆设裁剪区域 path = UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderW, borderW, image.size.width, image.size.height); path addClip; /6.把图片给绘制上下文. image drawInRect:CGRectMake(borderW,
4、borderW, image.size.width, image.size.height); /7.生成一张新的图片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); /8.关闭上下文. UIGraphicsEndImageContext(); / 抽取出来的功能 self.imageV.image = UIImage imageWithBorderWidth:10 borderColor:UIColor greenColor image:UIImage imageNamed:阿狸头像;- (UIImage *)i
5、mageWithBorderWidth:(CGFloat)borderW borderColor:(UIColor *)color image:(UIImage *)image /3.开启图片上下文. CGSize size = CGSizeMake(image.size.width + 2 * borderW, image.size.height + 2 * borderW); UIGraphicsBeginImageContextWithOptions(size, NO, 0); /4.先描述一个大圆,设为填充 UIBezierPath *path = UIBezierPath bezie
6、rPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height); /设置边框的颜色 color set; path fill; /5.再添加一个小圆,把小圆设裁剪区域 path = UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderW, borderW, image.size.width, image.size.height); path addClip; /6.把图片给绘制上下文. image drawInRect:CGRectMake(borderW, borderW,
7、image.size.width, image.size.height); /7.生成一张新的图片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); /8.关闭上下文. UIGraphicsEndImageContext(); return newImage; - (void)didReceiveMemoryWarning super didReceiveMemoryWarning; / Dispose of any resources that can be recreated.end/ UIImage+imag
8、e.m/ 01-带有边框的图片裁剪/ Created by Gavin on 15/9/14./ Copyright (c) 2015年 Gavin. All rights reserved./#import UIImage+image.himplementation UIImage (image)+ (UIImage *)imageWithBorderWidth:(CGFloat)borderW borderColor:(UIColor *)color image:(UIImage *)image /3.开启图片上下文. CGSize size = CGSizeMake(image.size
9、.width + 2 * borderW, image.size.height + 2 * borderW); UIGraphicsBeginImageContextWithOptions(size, NO, 0); /4.先描述一个大圆,设为填充 UIBezierPath *path = UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height); /设置边框的颜色 color set; path fill; /5.再添加一个小圆,把小圆设裁剪区域 path = UIBezierPath be
10、zierPathWithOvalInRect:CGRectMake(borderW, borderW, image.size.width, image.size.height); path addClip; /6.把图片给绘制上下文. image drawInRect:CGRectMake(borderW, borderW, image.size.width, image.size.height); /7.生成一张新的图片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); /8.关闭上下文. UIGraphicsE
11、ndImageContext(); return newImage; end/ ViewController.m/ 02-截屏/ Created by Gavin on 15/9/14./ Copyright (c) 2015年 Gavin. All rights reserved./#import ViewController.hinterface ViewController ()endimplementation ViewController- (void)viewDidLoad super viewDidLoad; / Do any additional setup after loa
12、ding the view, typically from a nib.-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event /1.开启图片上下文. UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0); /获取当前的上下文. CGContextRef ctx = UIGraphicsGetCurrentContext(); /UIView之所能够显示,是因为它内部有一个层,layer.层是通过渲染的方法,给绘制上下文. self.vie
13、w.layer renderInContext:ctx; /生成一张图片. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); /如何把图片转换成二进流. NSData *data = UIImagePNGRepresentation(newImage); data writeToFile:/Users/gaoxinqiang/Desktop/newImage.png atomically:YES; /关闭上下文. UIGraphicsEndImageContext(); - (void)didReceiveMemo
14、ryWarning super didReceiveMemoryWarning; / Dispose of any resources that can be recreated.end/ ViewController.m/ 03-图片截屏/ Created by Gavin on 15/9/14./ Copyright (c) 2015年 Gavin. All rights reserved./#import ViewController.hinterface ViewController ()/手指开始点property(nonatomic,assign)CGPoint startP;/遮
15、盖property(nonatomic,weak) UIView *coverView;property (weak, nonatomic) IBOutlet UIImageView *imageV;endimplementation ViewController/懒加载遮盖,保存遮盖只创建一次.-(UIView *)coverView if (_coverView = nil) UIView *view = UIView alloc init; view.backgroundColor = UIColor blackColor; view.alpha = 0.7; self.view add
16、Subview:view; _coverView = view; return _coverView;- (void)viewDidLoad super viewDidLoad; / Do any additional setup after loading the view, typically from a nib.- (IBAction)pan:(UIPanGestureRecognizer *)pan /获取当前手指所在的点. CGPoint curP = pan locationInView:self.view; if(pan.state = UIGestureRecognizerS
17、tateBegan) CGPoint startP = curP; self.startP = startP; else if(pan.state = UIGestureRecognizerStateChanged) CGFloat offsetX = curP.x - self.startP.x; CGFloat offsetY = curP.y - self.startP.y; /确定遮盖的区域位置 CGRect rect = CGRectMake(self.startP.x, self.startP.y, offsetX, offsetY); /每次移动时,调用遮盖的Frame. sel
18、f.coverView.frame = rect; else if(pan.state = UIGestureRecognizerStateEnded) /1.开启一个跟原始图片相同大小的图片上下文. UIGraphicsBeginImageContextWithOptions(self.imageV.bounds.size, NO, 0); /把self.cover.frame 设为裁剪区域 UIBezierPath *path = UIBezierPath bezierPathWithRect:self.coverView.frame; path addClip; /获取当前上下文. CG
19、ContextRef ctx = UIGraphicsGetCurrentContext(); /把UIImageView当中的图片给绘制上下文当中. self.imageV.layer renderInContext:ctx; /从上下文当中生成一张新的图片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); /关闭上下文. UIGraphicsEndImageContext(); /给图片重新赋值 self.imageV.image = newImage; /移除遮盖 self.coverView removeF
20、romSuperview; - (void)didReceiveMemoryWarning super didReceiveMemoryWarning; / Dispose of any resources that can be recreated.end/ ViewController.m/ 04-图片擦除/ Created by Gavin on 15/9/14./ Copyright (c) 2015年 Gavin. All rights reserved./#import ViewController.hinterface ViewController ()property (wea
21、k, nonatomic) IBOutlet UIImageView *imageV;endimplementation ViewController- (void)viewDidLoad super viewDidLoad; / Do any additional setup after loading the view, typically from a nib. self.imageV.userInteractionEnabled = YES; /添加手势 UIPanGestureRecognizer *pan = UIPanGestureRecognizer alloc initWit
22、hTarget:self action:selector(pan:); self.imageV addGestureRecognizer:pan;- (void)pan:(UIPanGestureRecognizer *)pan /获取当前手指所在的点 CGPoint curP = pan locationInView:self.imageV; CGFloat rectWH = 50; CGFloat x = curP.x - rectWH * 0.5; CGFloat y = curP.y - rectWH * 0.5; CGRect rect = CGRectMake(x, y, rect
23、WH, rectWH); /开启一个图片上下文. UIGraphicsBeginImageContextWithOptions(self.imageV.bounds.size, NO, 0); /获取当前的上下文. CGContextRef ctx = UIGraphicsGetCurrentContext(); /把UImageViwe上面的图片给绘制到上下文. self.imageV.layer renderInContext:ctx; /确定擦除区域 CGContextClearRect(ctx, rect); /生成一张新图片 UIImage *newImage = UIGraphic
24、sGetImageFromCurrentImageContext(); /把上下文给关闭 UIGraphicsEndImageContext(); /给原来图片重新赋值 self.imageV.image = newImage; - (void)didReceiveMemoryWarning super didReceiveMemoryWarning; / Dispose of any resources that can be recreated.end/ ClockView.m/ 05-手势解锁/ Created by Gavin on 15/9/14./ Copyright (c) 20
25、15年 Gavin. All rights reserved./#import ClockView.himplementation ClockView-(void)awakeFromNib /添加按钮 self setUP;- (instancetype)initWithFrame:(CGRect)frame if (self = super initWithFrame:frame) /添加按钮 self setUP; return self;/添加按钮- (void)setUP for (int i = 0; i 9; i+) /创建按钮 UIButton *btn = UIButton b
26、uttonWithType:UIButtonTypeCustom; btn.userInteractionEnabled = NO; btn setImage:UIImage imageNamed:gesture_node_normal forState:UIControlStateNormal; btn setImage:UIImage imageNamed:gesture_node_highlighted forState:UIControlStateSelected; self addSubview:btn; /获取当前手指所在的点- (CGPoint)getCurrentPoint:(
27、NSSet *)touches /获取当前手指所在的点 UITouch *touch = touches anyObject; return touch locationInView:self;/判断当前手指在不在按钮上.- (UIButton *)btnRectContainsPoint:(CGPoint)point for (UIButton *btn in self.subviews) /判断当前手指在不在按钮上. /判断一个点在不在某个区域当中 if (CGRectContainsPoint(btn.frame, point) return btn; return nil; /手指开始
28、点击-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event NSLog(%s,_func_); CGPoint curP = self getCurrentPoint:touches; UIButton *btn = self btnRectContainsPoint:curP; if (btn) btn.selected = YES; /手指移动的时候调用-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event /获取当前手指所在的点 CGPoint
29、curP = self getCurrentPoint:touches; UIButton *btn = self btnRectContainsPoint:curP; if (btn) btn.selected = YES; /手指离开屏幕时调用- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event -(void)layoutSubviews super layoutSubviews; int clomn = 3; CGFloat x = 0; CGFloat y = 0; CGFloat btnWH = 74; CGFloat margin = (self.bounds.size.width - btnWH * cl
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1