ios绘图.docx
《ios绘图.docx》由会员分享,可在线阅读,更多相关《ios绘图.docx(21页珍藏版)》请在冰豆网上搜索。
ios绘图
//
//ViewController.m
//01-带有边框的图片裁剪
//
//CreatedbyGavinon15/9/14.
//Copyright(c)2015年Gavin.Allrightsreserved.
//
#import"ViewController.h"
#import"UIImage+image.h"
@interfaceViewController()
@property(weak,nonatomic)IBOutletUIImageView*imageV;
@end
@implementationViewController
-(void)viewDidLoad{
[superviewDidLoad];
//1.加载
UIImage*image=[UIImageimageNamed:
@"阿狸头像"];
//2.边框宽度
CGFloatborderW=10;
//3.开启图片上下文.
CGSizesize=CGSizeMake(image.size.width+2*borderW,image.size.height+2*borderW);
UIGraphicsBeginImageContextWithOptions(size,NO,0);
//4.先描述一个大圆,设为填充
UIBezierPath*path=[UIBezierPathbezierPathWithOvalInRect:
CGRectMake(0,0,size.width,size.height)];
[[UIColorredColor]set];
[pathfill];
//5.再添加一个小圆,把小圆设裁剪区域
path=[UIBezierPathbezierPathWithOvalInRect:
CGRectMake(borderW,borderW,image.size.width,image.size.height)];
[pathaddClip];
//6.把图片给绘制上下文.
[imagedrawInRect:
CGRectMake(borderW,borderW,image.size.width,image.size.height)];
//7.生成一张新的图片
UIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();
//8.关闭上下文.
UIGraphicsEndImageContext();
//抽取出来的功能
self.imageV.image=[UIImageimageWithBorderWidth:
10borderColor:
[UIColorgreenColor]image:
[UIImageimageNamed:
@"阿狸头像"]];
}
-(UIImage*)imageWithBorderWidth:
(CGFloat)borderWborderColor:
(UIColor*)colorimage:
(UIImage*)image{
//3.开启图片上下文.
CGSizesize=CGSizeMake(image.size.width+2*borderW,image.size.height+2*borderW);
UIGraphicsBeginImageContextWithOptions(size,NO,0);
//4.先描述一个大圆,设为填充
UIBezierPath*path=[UIBezierPathbezierPathWithOvalInRect:
CGRectMake(0,0,size.width,size.height)];
//设置边框的颜色
[colorset];
[pathfill];
//5.再添加一个小圆,把小圆设裁剪区域
path=[UIBezierPathbezierPathWithOvalInRect:
CGRectMake(borderW,borderW,image.size.width,image.size.height)];
[pathaddClip];
//6.把图片给绘制上下文.
[imagedrawInRect:
CGRectMake(borderW,borderW,image.size.width,image.size.height)];
//7.生成一张新的图片
UIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();
//8.关闭上下文.
UIGraphicsEndImageContext();
returnnewImage;
}
-(void)didReceiveMemoryWarning{
[superdidReceiveMemoryWarning];
//Disposeofanyresourcesthatcanberecreated.
}
@end
//
//UIImage+image.m
//01-带有边框的图片裁剪
//
//CreatedbyGavinon15/9/14.
//Copyright(c)2015年Gavin.Allrightsreserved.
//
#import"UIImage+image.h"
@implementationUIImage(image)
+(UIImage*)imageWithBorderWidth:
(CGFloat)borderWborderColor:
(UIColor*)colorimage:
(UIImage*)image{
//3.开启图片上下文.
CGSizesize=CGSizeMake(image.size.width+2*borderW,image.size.height+2*borderW);
UIGraphicsBeginImageContextWithOptions(size,NO,0);
//4.先描述一个大圆,设为填充
UIBezierPath*path=[UIBezierPathbezierPathWithOvalInRect:
CGRectMake(0,0,size.width,size.height)];
//设置边框的颜色
[colorset];
[pathfill];
//5.再添加一个小圆,把小圆设裁剪区域
path=[UIBezierPathbezierPathWithOvalInRect:
CGRectMake(borderW,borderW,image.size.width,image.size.height)];
[pathaddClip];
//6.把图片给绘制上下文.
[imagedrawInRect:
CGRectMake(borderW,borderW,image.size.width,image.size.height)];
//7.生成一张新的图片
UIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();
//8.关闭上下文.
UIGraphicsEndImageContext();
returnnewImage;
}
@end
//
//ViewController.m
//02-截屏
//
//CreatedbyGavinon15/9/14.
//Copyright(c)2015年Gavin.Allrightsreserved.
//
#import"ViewController.h"
@interfaceViewController()
@end
@implementationViewController
-(void)viewDidLoad{
[superviewDidLoad];
//Doanyadditionalsetupafterloadingtheview,typicallyfromanib.
}
-(void)touchesBegan:
(NSSet*)toucheswithEvent:
(UIEvent*)event{
//1.开启图片上下文.
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,NO,0);
//获取当前的上下文.
CGContextRefctx=UIGraphicsGetCurrentContext();
//UIView之所能够显示,是因为它内部有一个层,layer.层是通过渲染的方法,给绘制上下文.
[self.view.layerrenderInContext:
ctx];
//生成一张图片.
UIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();
//如何把图片转换成二进流.
NSData*data=UIImagePNGRepresentation(newImage);
[datawriteToFile:
@"/Users/gaoxinqiang/Desktop/newImage.png"atomically:
YES];
//关闭上下文.
UIGraphicsEndImageContext();
}
-(void)didReceiveMemoryWarning{
[superdidReceiveMemoryWarning];
//Disposeofanyresourcesthatcanberecreated.
}
@end
//
//ViewController.m
//03-图片截屏
//
//CreatedbyGavinon15/9/14.
//Copyright(c)2015年Gavin.Allrightsreserved.
//
#import"ViewController.h"
@interfaceViewController()
//手指开始点
@property(nonatomic,assign)CGPointstartP;
//遮盖
@property(nonatomic,weak)UIView*coverView;
@property(weak,nonatomic)IBOutletUIImageView*imageV;
@end
@implementationViewController
//懒加载遮盖,保存遮盖只创建一次.
-(UIView*)coverView{
if(_coverView==nil){
UIView*view=[[UIViewalloc]init];
view.backgroundColor=[UIColorblackColor];
view.alpha=0.7;
[self.viewaddSubview:
view];
_coverView=view;
}
return_coverView;
}
-(void)viewDidLoad{
[superviewDidLoad];
//Doanyadditionalsetupafterloadingtheview,typicallyfromanib.
}
-(IBAction)pan:
(UIPanGestureRecognizer*)pan{
//获取当前手指所在的点.
CGPointcurP=[panlocationInView:
self.view];
if(pan.state==UIGestureRecognizerStateBegan){
CGPointstartP=curP;
self.startP=startP;
}elseif(pan.state==UIGestureRecognizerStateChanged){
CGFloatoffsetX=curP.x-self.startP.x;
CGFloatoffsetY=curP.y-self.startP.y;
//确定遮盖的区域位置
CGRectrect=CGRectMake(self.startP.x,self.startP.y,offsetX,offsetY);
//每次移动时,调用遮盖的Frame.
self.coverView.frame=rect;
}elseif(pan.state==UIGestureRecognizerStateEnded){
//1.开启一个跟原始图片相同大小的图片上下文.
UIGraphicsBeginImageContextWithOptions(self.imageV.bounds.size,NO,0);
//把self.cover.frame设为裁剪区域
UIBezierPath*path=[UIBezierPathbezierPathWithRect:
self.coverView.frame];
[pathaddClip];
//获取当前上下文.
CGContextRefctx=UIGraphicsGetCurrentContext();
//把UIImageView当中的图片给绘制上下文当中.
[self.imageV.layerrenderInContext:
ctx];
//从上下文当中生成一张新的图片
UIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();
//关闭上下文.
UIGraphicsEndImageContext();
//给图片重新赋值
self.imageV.image=newImage;
//移除遮盖
[self.coverViewremoveFromSuperview];
}
}
-(void)didReceiveMemoryWarning{
[superdidReceiveMemoryWarning];
//Disposeofanyresourcesthatcanberecreated.
}
@end
//
//ViewController.m
//04-图片擦除
//
//CreatedbyGavinon15/9/14.
//Copyright(c)2015年Gavin.Allrightsreserved.
//
#import"ViewController.h"
@interfaceViewController()
@property(weak,nonatomic)IBOutletUIImageView*imageV;
@end
@implementationViewController
-(void)viewDidLoad{
[superviewDidLoad];
//Doanyadditionalsetupafterloadingtheview,typicallyfromanib.
self.imageV.userInteractionEnabled=YES;
//添加手势
UIPanGestureRecognizer*pan=[[UIPanGestureRecognizeralloc]initWithTarget:
selfaction:
@selector(pan:
)];
[self.imageVaddGestureRecognizer:
pan];
}
-(void)pan:
(UIPanGestureRecognizer*)pan{
//获取当前手指所在的点
CGPointcurP=[panlocationInView:
self.imageV];
CGFloatrectWH=50;
CGFloatx=curP.x-rectWH*0.5;
CGFloaty=curP.y-rectWH*0.5;
CGRectrect=CGRectMake(x,y,rectWH,rectWH);
//开启一个图片上下文.
UIGraphicsBeginImageContextWithOptions(self.imageV.bounds.size,NO,0);
//获取当前的上下文.
CGContextRefctx=UIGraphicsGetCurrentContext();
//把UImageViwe上面的图片给绘制到上下文.
[self.imageV.layerrenderInContext:
ctx];
//确定擦除区域
CGContextClearRect(ctx,rect);
//生成一张新图片
UIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();
//把上下文给关闭
UIGraphicsEndImageContext();
//给原来图片重新赋值
self.imageV.image=newImage;
}
-(void)didReceiveMemoryWarning{
[superdidReceiveMemoryWarning];
//Disposeofanyresourcesthatcanberecreated.
}
@end
//
//ClockView.m
//05-手势解锁
//
//CreatedbyGavinon15/9/14.
//Copyright(c)2015年Gavin.Allrightsreserved.
//
#import"ClockView.h"
@implementationClockView
-(void)awakeFromNib{
//添加按钮
[selfsetUP];
}
-(instancetype)initWithFrame:
(CGRect)frame{
if(self=[superinitWithFrame:
frame]){
//添加按钮
[selfsetUP];
}
returnself;
}
//添加按钮
-(void)setUP{
for(inti=0;i<9;i++){
//创建按钮
UIButton*btn=[UIButtonbuttonWithType:
UIButtonTypeCustom];
btn.userInteractionEnabled=NO;
[btnsetImage:
[UIImageimageNamed:
@"gesture_node_normal"]forState:
UIControlStateNormal];
[btnsetImage:
[UIImageimageNamed:
@"gesture_node_highlighted"]forState:
UIControlStateSelected];
[selfaddSubview:
btn];
}
}
//获取当前手指所在的点
-(CGPoint)getCurrentPoint:
(NSSet*)touches{
//获取当前手指所在的点
UITouch*touch=[touchesanyObject];
return[touchlocationInView:
self];
}
//判断当前手指在不在按钮上.
-(UIButton*)btnRectContainsPoint:
(CGPoint)point{
for(UIButton*btninself.subviews){
//判断当前手指在不在按钮上.
//判断一个点在不在某个区域当中
if(CGRectContainsPoint(btn.frame,point)){
returnbtn;
}
}
returnnil;
}
//手指开始点击
-(void)touchesBegan:
(NSSet*)toucheswithEvent:
(UIEvent*)event{
NSLog(@"%s",__func__);
CGPointcurP=[selfgetCurrentPoint:
touches];
UIButton*btn=[selfbtnRectContainsPoint:
curP];
if(btn){
btn.selected=YES;
}
}
//手指移动的时候调用
-(void)touchesMoved:
(NSSet*)toucheswithEvent:
(UIEvent*)event{
//获取当前手指所在的点
CGPointcurP=[selfgetCurrentPoint:
touches];
UIButton*btn=[selfbtnRectContainsPoint:
curP];
if(btn){
btn.selected=YES;
}
}
//手指离开屏幕时调用
-(void)touchesEnded:
(NSSet*)toucheswithEvent:
(UIEvent*)event{
}
-(void)layoutSubviews{
[superlayoutSubviews];
intclomn=3;
CGFloatx=0;
CGFloaty=0;
CGFloatbtnWH=74;
CGFloatmargin=(self.bounds.size.width-btnWH*cl