- (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];
//第二個(gè)扇形
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;
//調(diào)用drawRect
//[self drawRect:self.bounds];
//當(dāng)系統(tǒng)自動(dòng)調(diào)用drawRect方法時(shí)會(huì)自動(dòng)創(chuàng)建跟View相關(guān)聯(lián)的上下文
//重繪setNeedsDisplay 系統(tǒng)會(huì)自動(dòng)調(diào)用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.把上下文的內(nèi)容渲染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.創(chuàng)建位圖上下文(size:開(kāi)啟多大的上下文,就會(huì)生成多大的圖片)
UIGraphicsBeginImageContext(oriImage.size);
//2.把圖片繪制到上下文當(dāng)中
[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.從上下文當(dāng)中生成一張圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//5.關(guān)閉位圖上下文
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.開(kāi)啟位圖上下文(大小 原始圖片的寬高度+ 2 *邊框?qū)挾?
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.繪制小圓(把小圓設(shè)置成裁剪區(qū)域)
UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderW, borderW, oriImage.size.width, oriImage.size.height)];
[clipPath addClip];
//6.把圖片繪制到上下文當(dāng)中
[oriImage drawAtPoint:CGPointMake(borderW, borderW)];
//7.從上下文當(dāng)中生成圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//8.關(guān)閉上下文.
UIGraphicsEndImageContext();
return newImage;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//生成圖片
//1.開(kāi)啟一個(gè)位圖上下文
//高清 (iOS 7之后)
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0);
//模糊
UIGraphicsBeginImageContext(self.view.bounds.size);
//2.把View的內(nèi)容繪制到上下文當(dāng)中
CGContextRef ctx = UIGraphicsGetCurrentContext();
//UIView內(nèi)容想要繪制到上下文當(dāng)中, 必須使用渲染的方式
[self.view.layer renderInContext:ctx];
//3.從上下文當(dāng)中生成一張圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//4.關(guān)閉上下文
UIGraphicsEndImageContext();
//把圖片轉(zhuǎn)成二進(jìn)制流
//NSData *data = UIImageJPEGRepresentation(newImage, 1);
NSData *data = UIImagePNGRepresentation(newImage);
[data writeToFile:@"/Users/xiaomage/Desktop/newImage.png" atomically:YES];
}
- (圖片截屏)截取圖片一部分(手指滑動(dòng)截取)
#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.什么時(shí)候用到什么時(shí)候才去創(chuàng)建
// 2.保持當(dāng)前的View在內(nèi)存當(dāng)中只有一份.
// 3.保持用到View時(shí),肯定是有值的.
-(UIView *)coverView {
if (_coverView == nil) {
//創(chuàng)建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 {
//獲取當(dāng)前手指所在的點(diǎn)
CGPoint curP = [pan locationInView:self.imageV];
//判斷手勢(shì)的狀態(tài)
if(pan.state == UIGestureRecognizerStateBegan) {
//記錄當(dāng)前手指的開(kāi)始點(diǎn)
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);
//設(shè)置裁剪區(qū)域
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.coverView.frame];
[path addClip];
//2.把UIImageV當(dāng)中的內(nèi)容渲染到上下文當(dāng)中
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.imageV.layer renderInContext:ctx];
//從上下文當(dāng)中獲取圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//關(guān)閉上下文
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;
//添加手勢(shì)
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.imageV addGestureRecognizer:pan];
}
- (void)pan:(UIPanGestureRecognizer *)pan {
CGFloat rectWH = 20;
//獲取當(dāng)前手指的點(diǎn)
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);
//開(kāi)啟一個(gè)位圖上下文
//UIGraphicsBeginImageContext(self.imageV.bounds.size);
UIGraphicsBeginImageContextWithOptions(self.imageV.bounds.size, NO, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
//把UIImageV的內(nèi)容渲染到上下文當(dāng)中
[self.imageV.layer renderInContext:ctx];
//擦除上下文當(dāng)中指定的區(qū)域
CGContextClearRect(ctx, rect);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
self.imageV.image = newImage;
//關(guān)閉上下文
UIGraphicsEndImageContext();
}
//
// ClockView.m
// 07-手勢(shì)解鎖
//
// 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++) {
//創(chuàng)建按鈕
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.userInteractionEnabled = NO;
btn.tag = i;
//設(shè)置按鈕的圖片
[btn setImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
//設(shè)置選中狀態(tài)下的圖片
[btn setImage:[UIImage imageNamed:@"gesture_node_selected"] forState:UIControlStateSelected];
[self addSubview:btn];
}
}
//按功能模塊抽方法
//獲取當(dāng)前手指的點(diǎn)
- (CGPoint)getCurPoint:(NSSet *)touches {
//獲取當(dāng)前手指的點(diǎn)
UITouch *touch = [touches anyObject];
CGPoint curP = [touch locationInView:self];
return curP;
}
//給定一個(gè)點(diǎn),判斷這個(gè)點(diǎn)在不在按鈕身上
//如果沒(méi)有找到符合的條件,直接返回nil.
- (UIButton *)btnContainsPoint:(CGPoint)point {
//取出所有的子控件.
for (UIButton *btn in self.subviews) {
//判斷當(dāng)前點(diǎn)在不在按鈕身上.
if (CGRectContainsPoint(btn.frame, point)) {
//如果在的話, 讓按鈕成為選中狀態(tài)
return btn;
}
}
return nil;
}
//手指開(kāi)始點(diǎn)擊
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//獲取當(dāng)前手指的點(diǎn)
CGPoint curP = [self getCurPoint:touches];
//給定一個(gè)點(diǎn),判斷這個(gè)點(diǎn)在不在按鈕身上
UIButton *btn = [self btnContainsPoint:curP];
if(btn && btn.selected == NO) {
btn.selected = YES;
//保存選中的按鈕
[self.selectBtnArray addObject:btn];
}
}
//手指移動(dòng)
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//獲取當(dāng)前手指的點(diǎn)
CGPoint curP = [self getCurPoint:touches];
//記錄當(dāng)前手指的點(diǎn)
self.curP = curP;
//取出所有的子控件.
//給定一個(gè)點(diǎn),判斷這個(gè)點(diǎn)在不在按鈕身上
UIButton *btn = [self btnContainsPoint:curP];
if(btn && btn.selected == NO) {
btn.selected = YES;
//保存選中的按鈕
[self.selectBtnArray addObject:btn];
}
//重繪
[self setNeedsDisplay];
}
//手指離開(kāi)
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//所有選中按鈕取消選中狀態(tài)
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++) {
//取出每一個(gè)按鈕
UIButton *btn = self.selectBtnArray[i];
//如果說(shuō)按鈕是第一個(gè),讓按鈕的中心點(diǎn)是路徑的起點(diǎn).
if (i == 0) {
[path moveToPoint:btn.center];
}else {
[path addLineToPoint:btn.center];
}
}
//添加一根線到當(dāng)前手指所在的點(diǎn)
[path addLineToPoint:self.curP];
//設(shè)置線的狀態(tài)
[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;
//取出每一個(gè)字控件,設(shè)置frame
for (int i = 0 ; i < self.subviews.count; i++) {
//當(dāng)前所在的列
curColumn = i % column;
//當(dāng)前所在的行
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.
}
//屬于誰(shuí)的事, 誰(shuí)來(lái)做
//清屏
- (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];
//設(shè)置照片來(lái)源
pickVC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//設(shè)置代理
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 {
//對(duì)畫板作截屏
//1.開(kāi)啟一個(gè)位圖上下文
UIGraphicsBeginImageContext(self.drawView.bounds.size);
//2.把畫板的內(nèi)容渲染到上下文當(dāng)中.
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.drawView.layer renderInContext:ctx];
//3.從上下文當(dāng)中取出一張圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//4.關(guān)閉上下文
UIGraphicsEndImageContext();
//5.把生成的圖片寫入到系統(tǒng)相冊(cè)當(dāng)中
//注意:寫放完成時(shí)調(diào)用的方法必須得是didFinishSavingWithError;
UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
//當(dāng)寫入完成時(shí)調(diào)用
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
NSLog(@"%s",__func__);
}
- (void)success {
}
//設(shè)置線寬度
- (IBAction)setLineWith:(UISlider *)sender {
[self.drawView setLineWidth:sender.value];
}
//設(shè)置線的顏色
- (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;
//設(shè)置線寬度
- (void)setLineWidth:(CGFloat)width;
//設(shè)置線的顏色
- (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 {
//刪除最后一個(gè)路徑
[self.pathArray removeLastObject];
//重繪
[self setNeedsDisplay];
}
//橡皮擦
- (void)erase {
[self setLineColor:[UIColor whiteColor]];
}
//設(shè)置線寬度
- (void)setLineWidth:(CGFloat)width {
self.width = width;
}
////設(shè)置線的顏色
- (void)setLineColor:(UIColor *)color {
self.color = color;
}
- (NSMutableArray *)pathArray {
if (_pathArray == nil) {
_pathArray = [NSMutableArray array];
}
return _pathArray;
}
- (void)awakeFromNib {
//添加手勢(shì)
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self addGestureRecognizer:pan];
self.width = 1;
self.color = [UIColor blackColor];
}
- (void)pan:(UIPanGestureRecognizer *)pan {
//畫線
//獲取當(dāng)前手指的點(diǎn)
CGPoint curP = [pan locationInView:self];
if (pan.state == UIGestureRecognizerStateBegan) {
//創(chuàng)建路徑
//如果發(fā)現(xiàn)系統(tǒng)的類型沒(méi)有辦法瞞足要求時(shí),自定義類.繼承原來(lái)的類,在原來(lái)類的基礎(chǔ)上,添加屬于自己的東西.
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