iOS 图片截取
来源:互联网 发布:淘宝的3c证书编号 编辑:程序博客网 时间:2024/06/05 09:20
关于图片截取效果
1、
2、
3、
思路:
1、获取手指触屏的一个点
2、随着手指触屏的移动矩形大小随之变化
3、手指离开的时候绘制图片
4、创建当前矩形的上下文
5、确定裁剪范围
6、获取图片
7、关闭上下文
代码:
@interface DrawPictureVC ()@property (weak, nonatomic) IBOutlet UIView *backView;@property (weak, nonatomic) IBOutlet UIImageView *imageView;@property (nonatomic,strong) UIView *clipView;@property (nonatomic,assign) CGPoint startP;@end@implementation DrawPictureVC- (void)viewDidLoad { [super viewDidLoad]; self.title = @"draw Picture"; //给view添加一个是手势 UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]init]; [panGesture addTarget:self action:@selector(pan:)]; [self.backView addGestureRecognizer:panGesture];}-(UIView *)clipView{ if (_clipView == nil) { _clipView = [[UIView alloc]init]; [_clipView setBackgroundColor:[UIColor grayColor]]; [_clipView setAlpha:0.5]; [self.backView addSubview:_clipView]; } return _clipView;}-(void)pan:(UIPanGestureRecognizer *)pan{ CGPoint endP = CGPointZero; if (pan.state == UIGestureRecognizerStateBegan){ //开始拖动的时候,获取开始点 self.startP = [pan locationInView:self.backView]; }else if (pan.state == UIGestureRecognizerStateChanged){ //一直处于拖动的时候,获取变化点 endP = [pan locationInView:self.backView]; CGFloat w = endP.x - self.startP.x; CGFloat h = endP.y - self.startP.y; //生成矩形变化范围 [self.clipView setFrame:CGRectMake(self.startP.x, self.startP.y, w, h)]; }else if (pan.state == UIGestureRecognizerStateEnded){ //停止拖动的时候 //创建位图上下文 UIGraphicsBeginImageContextWithOptions(self.backView.bounds.size, NO, 0); //设置裁剪区域 UIBezierPath *backPath = [UIBezierPath bezierPathWithRect:self.clipView.frame]; [backPath addClip]; //获取当前上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); //把控件上的内容渲染到上下文 [self.imageView.layer renderInContext:ctx]; //获取图片 self.imageView.image = UIGraphicsGetImageFromCurrentImageContext(); //关闭上下文 UIGraphicsEndImageContext(); //移除置nil [self.clipView removeFromSuperview]; self.clipView = nil; }}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
最后,附上相关的demo,Git:(https://github.com/hejiasu/Drawing)
阅读全文
0 0
- ios 图片的截取
- ios 图片的截取
- iOS截取图片
- IOS 截取任意图片
- iOS view截取图片
- ios图片截取
- iOS 截取原型图片
- iOS 图片截取
- 十一 iOS 图片截取
- IOS 图片 截取 和 合并
- iOS图片的截取(OC)
- iOS截取正六边形图片
- iOS截取图片的一部分
- iOS 截取view生成图片
- 【IOS】旋转图片和截取图片
- iOS 截取部分图片并显示
- iOS怎样截取屏幕并生成图片?
- iOS中截取屏幕中局部图片
- 解决安装photoshop提示”please uninstall and reinstall the product“
- 使用布局设计可伸缩的对话框
- 混合高斯模型(Mixtures of Gaussians)和EM算法
- 页面导航下拉栏思路
- 我是如何从软件测试员转软件开发工程师的?
- iOS 图片截取
- Firefox 火狐 浏览器下载文件文件名中文乱码解决
- 手动更新源列表文件解决Ubuntu的Unable to locate package无法更新源问题
- sql 用查询语句实现翻页功能
- struts2 action接收文件上传数据处理
- list四种遍历
- 【2017/6/13】2017年企业运维岗经典面试题(28题)
- kvm虚拟化环境安装
- Python图表绘制:matplotlib绘图库入门