畫板
#import "DrawView.h"
@interface DrawView ()
@property(nonatomic,retain)NSMutableArray * paths;
@property(nonatomic,retain)UIBezierPath *path;
@end
@implementation DrawView
-(NSMutableArray*)paths
{
if (!_paths)
{
_paths=[NSMutableArray array];
}
return _paths;
}
-(instancetype)initWithFrame:(CGRect)frame
{
if (self==[super initWithFrame:frame])
{
UIPanGestureRecognizer*pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[self addGestureRecognizer:pan];
}
return self;
}
-(void)pan:(UIPanGestureRecognizer*)pan
{
//獲取當(dāng)前手指觸摸點(diǎn)
CGPoint currentP = [pan locationInView:self];
//獲取開始點(diǎn)
if (pan.state == UIGestureRecognizerStateBegan)
{
_path = [UIBezierPath bezierPath];
//設(shè)置路徑的起點(diǎn)
[_path moveToPoint:currentP];
//保存描述好的路徑
[self.paths addObject:_path];
}
// 手指拖動(dòng) 添加線到當(dāng)前觸摸點(diǎn)
[_path addLineToPoint:currentP];
//重繪
[self setNeedsDisplay];
}
//只要調(diào)用這個(gè)方法买置,就會(huì)把之前繪制的東西全部清掉笆载,重新繪制
- (void)drawRect:(CGRect)rect
{
for (UIBezierPath*path in self.paths)
{
[path stroke];
}
}
圖片裁剪
@interface CompanyViewController ()
@property(nonatomic,retain)UIImageView*imageView;
@property(nonatomic,assign)CGPoint startP;
@property(nonatomic,retain)UIView*clipView;
@end
@implementation CompanyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
self.title=@"圖片裁剪";
_imageView=[[UIImageView alloc]initWithFrame:self.view.bounds];
_imageView.image=[UIImage imageNamed:@"qidong"];
[self.view addSubview:_imageView];
_clipView=[[UIView alloc]init];
_clipView.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.3];
[self.view addSubview:_clipView];
UIPanGestureRecognizer*pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[self.view addGestureRecognizer:pan];
}
-(void)pan:(UIPanGestureRecognizer*)pan
{
if (pan.state == UIGestureRecognizerStateBegan)
{
_startP = [pan locationInView:_imageView];
}
else if (pan.state == UIGestureRecognizerStateChanged)
{
CGPoint endA = [pan locationInView:_imageView];
CGFloat w = endA.x - _startP.x;
CGFloat h = endA.y - _startP.y;
//獲取截取范圍
CGRect clipRect = CGRectMake(_startP.x, _startP.y, w, h);
//生成截屏的view
self.clipView.frame = clipRect;
}
else if (pan.state == UIGestureRecognizerStateEnded)
{
//圖片裁剪贮缅,生成一張新的圖片
//開啟上下文
//如果不透明,默認(rèn)超出裁剪區(qū)域會(huì)變成黑色惦银,通常都是透明
UIGraphicsBeginImageContextWithOptions(_imageView.bounds.size, NO, 0);
//設(shè)置裁剪區(qū)域
UIBezierPath *path = [UIBezierPath bezierPathWithRect:_clipView.frame];
[path addClip];
//獲取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//把控件上的內(nèi)容渲染到上下文
[_imageView.layer renderInContext:context];
//生成一張新的圖片
_imageView.image = UIGraphicsGetImageFromCurrentImageContext();
//關(guān)閉
UIGraphicsEndImageContext();
}
}
圖片擦除
@interface CompanyViewController ()
@property(nonatomic,retain)UIImageView*imageView;
@end
@implementation CompanyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
self.title=@"圖片水印";
_imageView=[[UIImageView alloc]initWithFrame:self.view.bounds];
_imageView.image=[UIImage imageNamed:@""];
[self.view addGestureRecognizer:_imageView];
UIPanGestureRecognizer*pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[self.view addGestureRecognizer:pan];
}
-(void)pan:(UIPanGestureRecognizer*)pan
{
//獲取當(dāng)前點(diǎn)
CGPoint curentP = [pan locationInView:self.view];
//獲取擦除的矩形范圍
CGFloat wh = 30;
CGFloat x = curentP.x - wh*0.5;
CGFloat y = curentP.y - wh*0.5;
CGRect rect = CGRectMake(x, y, wh, wh);
//開啟上下文
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
//獲取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//控件的layer渲染上去
[_imageView.layer renderInContext:context];
//擦除圖片
CGContextClearRect(context, rect);
//生存一張圖片
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
_imageView.image = image;
//關(guān)閉上下文
UIGraphicsEndImageContext();
}
方形圖片生產(chǎn)圓形頭像
+(UIImage*)imageWithCilpImage:(UIImage*)image borderWidth:(CGFloat)borderWidth borderColor:(UIColor*)color
{
//圖片的寬度和高度
CGFloat imageWH = image.size.width;
//圓形的寬度和高度
CGFloat ovalWH = imageWH + 2*borderWidth;
//開啟上下文
UIGraphicsBeginImageContextWithOptions(CGSizeMake(ovalWH, ovalWH), NO, 0);
//畫大圓
UIBezierPath * path=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, ovalWH, ovalWH)];
//設(shè)置圓環(huán)顏色
[color set];
//顏色填充
[path fill];
//設(shè)置裁剪區(qū)域
UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderWidth, borderWidth, imageWH, imageWH)];
//圖片剪切
[clipPath addClip];
//繪制圖片
[image drawAtPoint:CGPointMake(borderWidth, borderWidth)];
//獲取圖片
UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();
//關(guān)閉上下文
UIGraphicsEndImageContext();
return clipImage;
}
手機(jī)截屏
+(UIImage*)imageWithCaputureView:(UIView*)view
{
//開啟位圖上下文
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0);
//獲取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//把控件上的圖層渲染到上下文,layer只能渲染
[view.layer renderInContext:context];
//生成一張圖片
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
//關(guān)閉上下文
UIGraphicsEndImageContext();
return image;
}
圖片水印
+(UIImage*)imageWithWaterImage:(UIImage*)image waterTitle:(NSString*)title waterAttributes:(NSDictionary*)attributes
{
//0.獲取上下文,之前的上下文都是在view的drawRect方法中獲取(跟view相關(guān)聯(lián)的上下文layer上下文)
//此時(shí)我們需要繪制圖片到新的圖片上僧诚,因此需要用到位圖上下文
//怎么獲取位圖上下文,注意位圖上下文的獲取方式跟layer上下文不一樣蝗碎,位圖上下文需要我們手動(dòng)創(chuàng)建湖笨。
//開啟一個(gè)位圖上下文,注意位圖上下文跟view無關(guān)聯(lián)蹦骑,所以不需要drawRect
//size:位圖上下文的尺寸(新圖片的尺寸)
//opaque:不透明度 YES:不透明 NO:透明 一般都是透明的上下文
//scale:縮放比例 0:不縮放
UIGraphicsBeginImageContextWithOptions(image.size,NO, 0);
//1.繪制原生的圖片
[image drawAtPoint:CGPointZero];
//2.給原生的圖片添加文字
NSString*str = title;
[str drawAtPoint:CGPointMake(image.size.width-80, image.size.height-30) withAttributes:attributes];
//3.從上下文獲取生成的圖片
UIImage*imageWater = UIGraphicsGetImageFromCurrentImageContext();
//4.關(guān)閉上下文
UIGraphicsEndImageContext();
return imageWater;
}