您的位置:首页 > 产品设计 > UI/UE

在UIView中裁剪需要位置生成图片

2016-02-13 14:24 543 查看
新年快乐 ,新年快乐~

已经大年初六了,今年回家感觉倍儿的累,每天就能睡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 试试)



感谢观看,学以致用更感谢~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: