在UIView中裁剪需要位置生成图片
2016-02-13 14:24
543 查看
新年快乐 ,新年快乐~
已经大年初六了,今年回家感觉倍儿的累,每天就能睡5、6个小时。虽然很累,但是我的体重却增加了,这会是新年的礼物么?
前言:之前的项目用到了,在相册中选取图片,然后由用户调到适合位置,截取图片,传递到相关页面。
这个功能其实相对还是比较简单的,苹果给我们做了很好的封装,写了个demo,一起来看看代码吧~。
效果图如下:
图一:点击截图之前
图二:为点击之后的效果
------------------------------------------------------------------------------------
如果你想把截图的范围改变成为需要的位置-那就需要改变需求的size (比如把_imgV_Top
改成 self.view 试试)
感谢观看,学以致用更感谢~
已经大年初六了,今年回家感觉倍儿的累,每天就能睡5、6个小时。虽然很累,但是我的体重却增加了,这会是新年的礼物么?
前言:之前的项目用到了,在相册中选取图片,然后由用户调到适合位置,截取图片,传递到相关页面。
这个功能其实相对还是比较简单的,苹果给我们做了很好的封装,写了个demo,一起来看看代码吧~。
#import "ViewController.h" @interface ViewController (){ UIImageView *_imgV_Top;//上半部分原始图片 UIImageView *_imgV_Down;//下半部分分切图后的效果图 } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [self makeUI]; // Do any additional setup after loading the view, typically from a nib. } #pragma mark 布局 - (void)makeUI{ //点击截图的按钮 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake(10, 64, 45, 30); [btn setTitle:@"切图" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(cutBtnDown) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; //上部imgV _imgV_Top = [[UIImageView alloc] initWithFrame:CGRectMake(0, btn.frame.size.height+btn.frame.origin.y+20, self.view.frame.size.width, 200)]; _imgV_Top.image = [UIImage imageNamed:@"2222.png"]; [self.view addSubview:_imgV_Top]; //下部imgV _imgV_Down = [[UIImageView alloc] initWithFrame:CGRectMake(0, _imgV_Top.frame.size.height+_imgV_Top.frame.origin.y+20, self.view.frame.size.width, 200)]; [self.view addSubview:_imgV_Down]; } - (void)cutBtnDown{ _imgV_Down.image = [self cutUIimage]; } #pragma mark 下一步时裁剪 - (UIImage*)cutUIimage{ /* _imgV_Top.frame.size 是控制矩形大小 NO,YES 是控制是否透明 2.0是清晰度 有人说0.0是自动没有感觉,可手动调试 */ UIGraphicsBeginImageContextWithOptions(_imgV_Top.frame.size, NO, 2.0); [_imgV_Top.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image;// 生成后 } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
效果图如下:
图一:点击截图之前
图二:为点击之后的效果
------------------------------------------------------------------------------------
如果你想把截图的范围改变成为需要的位置-那就需要改变需求的size (比如把_imgV_Top
改成 self.view 试试)
感谢观看,学以致用更感谢~
相关文章推荐
- Building Maintainable Software-java篇之Separate Concerns in Modules
- UI基础-02 Xcode中的iOS工程模板
- 9.UITableView的设置
- ObQueryNameString routine
- Building Maintainable Software-java篇之Keep Unit Interfaces Small
- uva11039 -Building designing
- UIImagePickerController页中,按钮显示中文问题
- [SPOJ375]Query on a tree
- Building Maintainable Software-java篇之Write Code Once
- MASQUERADE 和 SNAT的区别
- UI基础-01第一个iOS应用程序
- uva1121 - Subsequence
- Key-Value Observing
- Sidelinesoft NL5 Circuit Simulator 2.1.1
- poj2127Greatest Common Increasing Subsequence【LICS】
- zoj3545Rescue the Rabbit (AC自动机+状压dp+滚动数组)
- UITableView如何撤销移动操作
- UITableView如何撤销移动操作
- UITableView如何撤销移动操作
- Codeforces Round #342 (Div. 2) 625A Guest From the Past(贪心)