- (void)draPie {
CGPoint center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * .5);
CGFloat radius = self.bounds.size.width * 0.5 - 10;
CGFloat startA = 0;
CGFloat endA = 25 / 100.0 * M_PI * 2;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
[[UIColor redColor] set];
//添加一根线到圆心
[path addLineToPoint:center];
[path fill];
//第二个扇形
startA = endA;
CGFloat angle = 25 / 100.0 * M_PI * 2;
endA = startA + angle;
UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
[[UIColor greenColor] set];
//添加一根线到圆心
[path2 addLineToPoint:center];
[path2 fill];
startA = endA;
angle = 50 / 100.0 * M_PI * 2;
endA = startA + angle;
UIBezierPath *path3 = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
[[UIColor blueColor] set];
//添加一根线到圆心
[path3 addLineToPoint:center];
[path3 fill];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//重绘
[self setNeedsDisplay];
}
#import <UIKit/UIKit.h>
@interface ProgressView : UIView
@property(nonatomic, assign) CGFloat progress;
@end
#import "ProgressView.h"
@implementation ProgressView
-(void)setProgress:(CGFloat)progress {
_progress = progress;
//调用drawRect
//[self drawRect:self.bounds];
//当系统自动调用drawRect方法时会自动创建跟View相关联的上下文
//重绘setNeedsDisplay 系统会自动调用drawRect:
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
//画弧
//1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.描述路径
CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
CGFloat radius = rect.size.width * 0.5 - 10;
CGFloat startA = -M_PI_2;
CGFloat endA = startA + self.progress * M_PI * 2;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
//3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.把上下文的内容渲染View.
CGContextStrokePath(ctx);
}
@end
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//生成一张图片
//0.加载图片
UIImage *oriImage = [UIImage imageNamed:@"小黄人"];
//1.创建位图上下文(size:开启多大的上下文,就会生成多大的图片)
UIGraphicsBeginImageContext(oriImage.size);
//2.把图片绘制到上下文当中
[oriImage drawAtPoint:CGPointZero];
//3.绘制水印
NSString *str = @"清凉一夏";
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSFontAttributeName] = [UIFont systemFontOfSize:20];
dict[NSForegroundColorAttributeName] = [UIColor redColor];
[str drawAtPoint:CGPointZero withAttributes:dict];
//4.从上下文当中生成一张图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//5.关闭位图上下文
UIGraphicsEndImageContext();
self.imageV.image = newImage;
}
#import "UIImage+image.h"
@implementation UIImage (image)
+ (UIImage *)imageWithBorder:(CGFloat)borderW color:(UIColor *)boderColor image:(UIImage *)oriImage {
//1.确定边框的宽度
//CGFloat borderW = 10;
//2.加载图片
//UIImage *oriImage = [UIImage imageNamed:@"阿狸头像"];
//3.开启位图上下文(大小 原始图片的宽高度+ 2 *边框宽度)
CGSize size = CGSizeMake(oriImage.size.width + 2 * borderW, oriImage.size.height + 2 * borderW);
UIGraphicsBeginImageContext(size);
//4.绘制边框(大圆)
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)];
[boderColor set];
[path fill];
//5.绘制小圆(把小圆设置成裁剪区域)
UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderW, borderW, oriImage.size.width, oriImage.size.height)];
[clipPath addClip];
//6.把图片绘制到上下文当中
[oriImage drawAtPoint:CGPointMake(borderW, borderW)];
//7.从上下文当中生成图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//8.关闭上下文.
UIGraphicsEndImageContext();
return newImage;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//生成图片
//1.开启一个位图上下文
//高清 (iOS 7之后)
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0);
//模糊
UIGraphicsBeginImageContext(self.view.bounds.size);
//2.把View的内容绘制到上下文当中
CGContextRef ctx = UIGraphicsGetCurrentContext();
//UIView内容想要绘制到上下文当中, 必须使用渲染的方式
[self.view.layer renderInContext:ctx];
//3.从上下文当中生成一张图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//4.关闭上下文
UIGraphicsEndImageContext();
//把图片转成二进制流
//NSData *data = UIImageJPEGRepresentation(newImage, 1);
NSData *data = UIImagePNGRepresentation(newImage);
[data writeToFile:@"/Users/xiaomage/Desktop/newImage.png" atomically:YES];
}
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@property (nonatomic, assign)CGPoint startP;
@property (nonatomic, weak) UIView *coverView;
@end
@implementation ViewController
//懒加载:1.什么时候用到什么时候才去创建
// 2.保持当前的View在内存当中只有一份.
// 3.保持用到View时,肯定是有值的.
-(UIView *)coverView {
if (_coverView == nil) {
//创建UIView
UIView *coverView = [[UIView alloc] init];
coverView.backgroundColor = [UIColor blackColor];
coverView.alpha = 0.7;
_coverView = coverView;
[self.view addSubview:coverView];
}
return _coverView;
}
- (IBAction)pan:(UIPanGestureRecognizer *)pan {
//获取当前手指所在的点
CGPoint curP = [pan locationInView:self.imageV];
//判断手势的状态
if(pan.state == UIGestureRecognizerStateBegan) {
//记录当前手指的开始点
self.startP = curP;
} else if(pan.state == UIGestureRecognizerStateChanged) {
//rect
CGFloat w = curP.x - self.startP.x;
CGFloat h = curP.y - self.startP.y;
CGRect rect = CGRectMake(self.startP.x, self.startP.y, w, h);
self.coverView.frame = rect;
}else if(pan.state == UIGestureRecognizerStateEnded) {
//生成一张图片
UIGraphicsBeginImageContext(self.imageV.bounds.size);
//设置裁剪区域
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.coverView.frame];
[path addClip];
//2.把UIImageV当中的内容渲染到上下文当中
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.imageV.layer renderInContext:ctx];
//从上下文当中获取图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//关闭上下文
UIGraphicsEndImageContext();
self.imageV.image = newImage;
//移除灰色透明板
[self.coverView removeFromSuperview];
}
}
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imageV.userInteractionEnabled = YES;
//添加手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.imageV addGestureRecognizer:pan];
}
- (void)pan:(UIPanGestureRecognizer *)pan {
CGFloat rectWH = 20;
//获取当前手指的点
CGPoint curP = [pan locationInView:self.imageV];
CGFloat x = curP.x - rectWH * 0.5;
CGFloat y = curP.y - rectWH * 0.5;
CGRect rect = CGRectMake(x, y, rectWH, rectWH);
//开启一个位图上下文
//UIGraphicsBeginImageContext(self.imageV.bounds.size);
UIGraphicsBeginImageContextWithOptions(self.imageV.bounds.size, NO, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
//把UIImageV的内容渲染到上下文当中
[self.imageV.layer renderInContext:ctx];
//擦除上下文当中指定的区域
CGContextClearRect(ctx, rect);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
self.imageV.image = newImage;
//关闭上下文
UIGraphicsEndImageContext();
}
//
// ClockView.m
// 07-手势解锁
//
// Created by xiaomage on 16/2/28.
// Copyright © 2016年 小码哥. All rights reserved.
//
#import "ClockView.h"
@interface ClockView()
@property (nonatomic ,strong) NSMutableArray *selectBtnArray;
@property (nonatomic, assign) CGPoint curP;
@end
@implementation ClockView
- (NSMutableArray *)selectBtnArray {
if (_selectBtnArray == nil) {
_selectBtnArray = [NSMutableArray array];
}
return _selectBtnArray;
}
- (void)awakeFromNib {
//添加子控件
[self setUp];
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//添加子控件
[self setUp];
}
return self;
}
//添加子控件
- (void)setUp {
for (int i = 0; i < 9; i++) {
//创建按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.userInteractionEnabled = NO;
btn.tag = i;
//设置按钮的图片
[btn setImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
//设置选中状态下的图片
[btn setImage:[UIImage imageNamed:@"gesture_node_selected"] forState:UIControlStateSelected];
[self addSubview:btn];
}
}
//按功能模块抽方法
//获取当前手指的点
- (CGPoint)getCurPoint:(NSSet *)touches {
//获取当前手指的点
UITouch *touch = [touches anyObject];
CGPoint curP = [touch locationInView:self];
return curP;
}
//给定一个点,判断这个点在不在按钮身上
//如果没有找到符合的条件,直接返回nil.
- (UIButton *)btnContainsPoint:(CGPoint)point {
//取出所有的子控件.
for (UIButton *btn in self.subviews) {
//判断当前点在不在按钮身上.
if (CGRectContainsPoint(btn.frame, point)) {
//如果在的话, 让按钮成为选中状态
return btn;
}
}
return nil;
}
//手指开始点击
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//获取当前手指的点
CGPoint curP = [self getCurPoint:touches];
//给定一个点,判断这个点在不在按钮身上
UIButton *btn = [self btnContainsPoint:curP];
if(btn && btn.selected == NO) {
btn.selected = YES;
//保存选中的按钮
[self.selectBtnArray addObject:btn];
}
}
//手指移动
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//获取当前手指的点
CGPoint curP = [self getCurPoint:touches];
//记录当前手指的点
self.curP = curP;
//取出所有的子控件.
//给定一个点,判断这个点在不在按钮身上
UIButton *btn = [self btnContainsPoint:curP];
if(btn && btn.selected == NO) {
btn.selected = YES;
//保存选中的按钮
[self.selectBtnArray addObject:btn];
}
//重绘
[self setNeedsDisplay];
}
//手指离开
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//所有选中按钮取消选中状态
NSMutableString *str = [NSMutableString string];
for (UIButton *btn in self.selectBtnArray) {
btn.selected = NO;
[str appendFormat:@"%ld",btn.tag];
}
NSLog(@"%@",str);
//清空路径
[self.selectBtnArray removeAllObjects];
//重绘
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
if (self.selectBtnArray.count) {
//描述路径
UIBezierPath *path = [UIBezierPath bezierPath];
//取出所有选中的按钮
for (int i = 0; i < self.selectBtnArray.count; i++) {
//取出每一个按钮
UIButton *btn = self.selectBtnArray[i];
//如果说按钮是第一个,让按钮的中心点是路径的起点.
if (i == 0) {
[path moveToPoint:btn.center];
}else {
[path addLineToPoint:btn.center];
}
}
//添加一根线到当前手指所在的点
[path addLineToPoint:self.curP];
//设置线的状态
[path setLineWidth:10];
[[UIColor redColor] set];
[path setLineJoinStyle:kCGLineJoinRound];
[path stroke];
}
}
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat x = 0;
CGFloat y = 0;
CGFloat btnWH = 74;
int column = 3;
CGFloat margin = (self.bounds.size.width - column * btnWH) / (column + 1);
int curColumn = 0;
int curRow = 0;
//取出每一个字控件,设置frame
for (int i = 0 ; i < self.subviews.count; i++) {
//当前所在的列
curColumn = i % column;
//当前所在的行
curRow = i / column;
x = margin + (margin + btnWH) * curColumn;
y = margin + (margin + btnWH) * curRow;
//取出每一按钮
UIButton *btn = self.subviews[i];
btn.frame = CGRectMake(x, y, btnWH, btnWH);
}
}
#import "ViewController.h"
#import "DrawView.h"
@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet DrawView *drawView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//属于谁的事, 谁来做
//清屏
- (IBAction)clear:(id)sender {
[self.drawView clear];
}
//撤销
- (IBAction)undo:(id)sender {
[self.drawView undo];
}
//橡皮擦
- (IBAction)erase:(id)sender {
[self.drawView erase];
}
//选择照片
- (IBAction)photo:(id)sender {
UIImagePickerController *pickVC = [[UIImagePickerController alloc] init];
//设置照片来源
pickVC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//设置代理
pickVC.delegate = self;
[self presentViewController:pickVC animated:YES completion:nil];
}
//#pa - mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
NSLog(@"%@",info);
UIImage *image = info[UIImagePickerControllerOriginalImage];
// NSData *data = UIImagePNGRepresentation(image);
// [data writeToFile:@"/Users/xiaomage/Desktop/image.png" atomically:YES];
//
self.drawView.image = image;
[self dismissViewControllerAnimated:YES completion:nil];
}
//保存
- (IBAction)save:(id)sender {
//对画板作截屏
//1.开启一个位图上下文
UIGraphicsBeginImageContext(self.drawView.bounds.size);
//2.把画板的内容渲染到上下文当中.
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.drawView.layer renderInContext:ctx];
//3.从上下文当中取出一张图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//4.关闭上下文
UIGraphicsEndImageContext();
//5.把生成的图片写入到系统相册当中
//注意:写放完成时调用的方法必须得是didFinishSavingWithError;
UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
//当写入完成时调用
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
NSLog(@"%s",__func__);
}
- (void)success {
}
//设置线宽度
- (IBAction)setLineWith:(UISlider *)sender {
[self.drawView setLineWidth:sender.value];
}
//设置线的颜色
- (IBAction)setLineColor:(UIButton *)sender {
[self.drawView setLineColor:sender.backgroundColor];
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
@interface DrawView : UIView
//清屏
- (void)clear;
//撤销
- (void)undo;
//橡皮擦
- (void)erase;
//设置线宽度
- (void)setLineWidth:(CGFloat)width;
//设置线的颜色
- (void)setLineColor:(UIColor *)color;
/** <#注释#>*/
@property (nonatomic ,strong) UIImage *image;
@end
#import "DrawView.h"
#import "MyBezierPath.h"
@interface DrawView()
/** <#注释#>*/
@property (nonatomic ,strong) UIBezierPath *path;
/** <#注释#>*/
@property (nonatomic ,strong) NSMutableArray *pathArray;
@property (nonatomic , assign) CGFloat width;
/** <#注释#>*/
@property (nonatomic ,strong) UIColor *color;
@end
@implementation DrawView
- (void)setImage:(UIImage *)image {
_image = image;
[self.pathArray addObject:image];
//重绘
[self setNeedsDisplay];
}
//清屏
- (void)clear {
//清空所有的路径
[self.pathArray removeAllObjects];
//重绘
[self setNeedsDisplay];
}
//撤销
- (void)undo {
//删除最后一个路径
[self.pathArray removeLastObject];
//重绘
[self setNeedsDisplay];
}
//橡皮擦
- (void)erase {
[self setLineColor:[UIColor whiteColor]];
}
//设置线宽度
- (void)setLineWidth:(CGFloat)width {
self.width = width;
}
////设置线的颜色
- (void)setLineColor:(UIColor *)color {
self.color = color;
}
- (NSMutableArray *)pathArray {
if (_pathArray == nil) {
_pathArray = [NSMutableArray array];
}
return _pathArray;
}
- (void)awakeFromNib {
//添加手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self addGestureRecognizer:pan];
self.width = 1;
self.color = [UIColor blackColor];
}
- (void)pan:(UIPanGestureRecognizer *)pan {
//画线
//获取当前手指的点
CGPoint curP = [pan locationInView:self];
if (pan.state == UIGestureRecognizerStateBegan) {
//创建路径
//如果发现系统的类型没有办法瞒足要求时,自定义类.继承原来的类,在原来类的基础上,添加属于自己的东西.
MyBezierPath *path = [[MyBezierPath alloc] init];
[path setLineWidth:self.width];
[path setLineJoinStyle:kCGLineJoinRound];
[path setLineCapStyle:kCGLineCapRound];
path.color = self.color;
self.path = path;
[self.pathArray addObject:path];
[path moveToPoint:curP];
} else if (pan.state == UIGestureRecognizerStateChanged) {
[self.path addLineToPoint:curP];
//绘制路径
[self setNeedsDisplay];
}
}
- (void)drawRect:(CGRect)rect {
//绘制出保存的所有路径
for (MyBezierPath *path in self.pathArray) {
if ([path isKindOfClass:[UIImage class]]) {
UIImage *image = (UIImage *)path;
[image drawInRect:rect];
}else {
[path.color set];
[path stroke];
}
}
}
@end