// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
// ViewController.m
#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) {
// 生成一张图片
// 1.开启位图上下文
UIGraphicsBeginImageContext(self.imageV.bounds.size);
// 2.设置裁剪区域
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.coverView.frame];
[path addClip];
// 3.把UIImageV当中的内容渲染到上下文当中
// 拿到当前上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.imageV.layer renderInContext:ctx];
// 4.从上下文当中获取图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// 5.关闭上下文
UIGraphicsEndImageContext();
self.imageV.image = newImage;
[self.coverView removeFromSuperview];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 截屏
// [self screenCapture];
}
// 截屏
- (void)screenCapture {
// 生成图片
// 1.开启一个位图上下文
UIGraphicsBeginImageContext(self.view.bounds.size);
// 拿到当前上下文,即当前位图上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.把View的内容绘制到上下文当中
// UIView内容想要绘制到上下文当中, 必须使用渲染的方式
[self.view.layer renderInContext:ctx];
// 3.从上下文当中生成一张图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// 4.关闭上下文
UIGraphicsEndImageContext();
// 把图片转成二进制流
// NSData *data = UIImageJPEGRepresentation(newImage, 1); // 1代表不压缩
NSData *data = UIImagePNGRepresentation(newImage);
[data writeToFile:@"/Users/apple/Desktop/newImage.png" atomically:YES];
}
@end
// UIImage+Image.h
#import <UIKit/UIKit.h>
@interface UIImage (Image)
// 根据颜色生成一张尺寸为1*1的相同颜色图片
+ (UIImage *)imageWithColor:(UIColor *)color;
@end
// UIImage+Image.m
#import "UIImage+Image.h"
@implementation UIImage (Image)
+ (UIImage *)imageWithColor:(UIColor *)color {
// 描述矩形
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
// 开启位图上下文
UIGraphicsBeginImageContext(rect.size);
// 获取位图上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 使用color演示填充上下文
CGContextSetFillColorWithColor(context, [color CGColor]);
// 渲染上下文
CGContextFillRect(context, rect);
// 从上下文中获取图片
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
// 结束上下文
UIGraphicsEndImageContext();
return theImage;
}
@end