-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
UIView *BGView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 110+150+150)];
BGView.backgroundColor = [UIColor whiteColor];
UIImageView *topImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 110)];
topImageView.image = [UIImage imageNamed:@"topImage.png"];
[BGView addSubview:topImageView];
UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 110+20, 320-40, 150-40)];
textLabel.numberOfLines = 0;
textLabel.text = @"下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了,关键就是第三个参数";
// textLabel.backgroundColor = [UIColor whiteColor];
textLabel.textColor = [UIColor blueColor];
[BGView addSubview:textLabel];
UIImageView *bottomImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 110+150, 320, 150)];
bottomImageView.image = [UIImage imageNamed:@"bottomImage.png"];
[BGView addSubview:bottomImageView];
UIImageView *testImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 50, 320, 110+150+150)];
testImageView.image = [self makeImageWithView:BGView withSize:BGView.frame.size];;
[self.view addSubview:testImageView];
}
#pragma mark 生成image
- (UIImage *)makeImageWithView:(UIView *)view withSize:(CGSize)size
{
// 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了,关键就是第三个参数 [UIScreen mainScreen].scale。
UIGraphicsBeginImageContextWithOptions(size, YES, [UIScreen mainScreen].scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
网友评论