#import "ViewController.h" #import "Masonry.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //一般的view生成圖片方法 UIView *subview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame) - 0 * 2, CGRectGetHeight(self.view.frame) - 0 * 2)]; [self.view addSubview:subview]; // [self onUIImageSubViewWithView:subview]; // // UIGraphicsBeginImageContext(subview.bounds.size); // self.view.backgroundColor = [UIColor whiteColor]; // [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; // UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); // UIImageView *ig=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(subview.frame), CGRectGetHeight(subview.frame) )]; ig.backgroundColor = [UIColor yellowColor]; ig.image = [self onUIImageSubViewWithView:subview]; [subview addSubview:ig]; } //view 需要生成的圖片 - (UIImage *)onUIImageSubViewWithView:(UIView *)view { //view容器 UIView *subview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(view.frame), CGRectGetWidth(view.frame) * 5.5 /4)]; subview.backgroundColor = [UIColor greenColor]; [view addSubview:subview]; //主圖 UIImageView *mainimgv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(subview.frame), CGRectGetWidth(subview.frame))]; mainimgv.backgroundColor = [UIColor purpleColor]; [subview addSubview:mainimgv]; mainimgv.image = [UIImage imageNamed:@"1.png"]; //小圖標 UIImageView *xiaoimgv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(view.frame) / 20, CGRectGetWidth(view.frame) / 20)]; xiaoimgv.backgroundColor = [UIColor purpleColor]; [subview addSubview:xiaoimgv]; [xiaoimgv mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(mainimgv.mas_bottom).mas_offset(10); make.left.equalTo(subview).mas_offset(10); make.size.mas_offset(CGSizeMake(CGRectGetWidth(view.frame) / 20, CGRectGetWidth(view.frame) / 20)); }]; //簡介 UILabel *nametxt = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)]; nametxt.text = @" 阿斯頓發大發大發是的發送到發送到發送到附近阿薩德放假啊收到了飛機阿斯頓發卡就是大幅拉升的減肥啦聖誕節費拉達斯放假啊了"; nametxt.numberOfLines = 3; [subview addSubview:nametxt]; [nametxt mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(mainimgv.mas_bottom).mas_offset(10); make.left.equalTo(subview).mas_offset(10); make.width.mas_offset(CGRectGetWidth(mainimgv.frame) / 2); }]; //小itemview UIView *xiaosubview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, 20)]; xiaosubview.layer.masksToBounds = YES; [subview addSubview:xiaosubview]; xiaosubview.layer.borderColor = [UIColor redColor].CGColor; xiaosubview.layer.borderWidth = 0.5; [xiaosubview mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(nametxt.mas_bottom).mas_offset(10); make.left.equalTo(subview).mas_offset(10); make.size.mas_offset(CGSizeMake(60, 20)); }]; xiaosubview.layer.cornerRadius = CGRectGetHeight(xiaosubview.frame) / 10; //xiao uilatxt UILabel *xiaouilatxt = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 20, 20)]; xiaouilatxt.backgroundColor = [UIColor redColor]; xiaouilatxt.textColor = [UIColor whiteColor]; xiaouilatxt.font = [UIFont systemFontOfSize:11]; [xiaosubview addSubview:xiaouilatxt]; xiaouilatxt.text = @"券"; xiaouilatxt.textAlignment = NSTextAlignmentCenter; [xiaouilatxt mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(xiaosubview).mas_offset(0); make.left.equalTo(xiaosubview).mas_offset(0); make.bottom.equalTo(xiaosubview).mas_offset(0); make.width.mas_offset(CGRectGetWidth(xiaosubview.frame) / 3); }]; //xiao 優惠券 UILabel *xiaoquantxt = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)]; xiaoquantxt.text = @"¥50"; xiaoquantxt.font = [UIFont systemFontOfSize:11]; xiaoquantxt.textColor = [UIColor redColor]; [xiaosubview addSubview:xiaoquantxt]; xiaoquantxt.textAlignment = NSTextAlignmentCenter; [xiaoquantxt mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(xiaosubview).mas_offset(0); make.left.equalTo(xiaouilatxt.mas_right).mas_offset(0); make.bottom.equalTo(xiaosubview).mas_offset(0); make.width.mas_offset(CGRectGetWidth(xiaosubview.frame) * 2 / 3); }]; //優惠價 UILabel *pricetxt = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)]; pricetxt.text = @"¥50.0"; pricetxt.font = [UIFont systemFontOfSize:15]; pricetxt.textColor = [UIColor redColor]; [subview addSubview:pricetxt]; [pricetxt mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(xiaosubview.mas_bottom).mas_offset(10); make.left.equalTo(subview).mas_offset(10); make.bottom.equalTo(subview).mas_offset(-10); }]; //原價 UILabel *oldpricetxt = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)]; oldpricetxt.text = @"¥150.0"; oldpricetxt.font = [UIFont systemFontOfSize:13]; oldpricetxt.textColor = [UIColor grayColor]; [subview addSubview:oldpricetxt]; [oldpricetxt mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(xiaosubview.mas_bottom).mas_offset(12); make.left.equalTo(pricetxt.mas_right).mas_offset(5); make.bottom.equalTo(subview).mas_offset(-10); }]; //鏈接二維碼 CGFloat qrH = CGRectGetHeight(subview.frame) - CGRectGetHeight(mainimgv.frame) - 10 * 2; UIImageView *qrimgv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)]; qrimgv.backgroundColor = [UIColor yellowColor]; [subview addSubview:qrimgv]; [qrimgv mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(mainimgv.mas_bottom).mas_offset(10); make.right.equalTo(subview).mas_offset(-10); make.size.mas_offset(CGSizeMake(qrH, qrH)); }]; //生成圖片 return [self makeImageWithView:subview withSize:CGSizeMake(subview.frame.size.width, subview.frame.size.height)]; } //這個方法生成的圖片不太清晰 不過把這個方法修改一下 就可以了 #pragma mark 生成image - (UIImage *)makeImageWithView:(UIView *)view withSize:(CGSize)size { // 下面方法,第一個參數表示區域大小。第二個參數表示是否是非透明的。如果需要顯示半透明效果,需要傳NO,否則傳YES。第三個參數就是屏幕密度了,關鍵就是第三個參數 [UIScreen mainScreen].scale。 UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }
@end