图片拼接
/* Image 拼接
* masterImage 主图片
* headImage 头图片
* footImage 尾图片
*/
+ (UIImage *)addHeadImage:(UIImage *)headImage footImage:(UIImage *)footImage toMasterImage:(UIImage *)masterImage {
CGSize size;
size.width = masterImage.size.width;
CGFloat headHeight = !headImage? 0:size.width/headImage.size.width*headImage.size.height;
CGFloat footHeight = !footImage? 0:size.width/footImage.size.width*footImage.size.height;
// CGFloat headHeight = !headImage? 0:headImage.size.height/2.0;
// CGFloat footHeight = !footImage? 0:footImage.size.height/2.0;
size.height = masterImage.size.height + headHeight + footHeight;
UIGraphicsBeginImageContextWithOptions(size, YES, 0.0);
if (headImage)
[headImage drawInRect:CGRectMake(0, 0, masterImage.size.width, headHeight)];
[masterImage drawInRect:CGRectMake(0, headHeight, masterImage.size.width, masterImage.size.height)];
if (footImage)
[footImage drawInRect:CGRectMake(0, masterImage.size.height + headHeight, masterImage.size.width, footHeight)];
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
}
截图
/**
获取一个view的截图,注意,只能截取能看的见的部分
@param view view
@param size 截图大小
@return 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;
}
UIScroolView、UITableView截取长图
截取长图部分我采用的逻辑是,首先获取contentSize,将ScroolView或TableView的高度设置为contentSize大小,设置[[self.tableView layer] renderInContext:UIGraphicsGetCurrentContext()];
然后截取图片,注意:在重设ScroolView或TableView的高度后,延迟执行了截图方法,因为立马截图超出视图部分的截图无法截出
/**
点击分享label
*/
- (void)clickShareLabel
{
//重新设置控件的大小,以能显示所有内容
[self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.right.offset(0);
make.top.equalTo(self.customNavBar.mas_bottom);
make.height.offset(self.tableView.contentSize.height);
}];
//延迟截屏
[self performSelector:@selector(beginCapture) withObject:nil afterDelay:0.1];
}
#pragma mark 生成image
- (UIImage *)captureImage
{
//设置截屏大小
UIGraphicsBeginImageContextWithOptions(self.tableView.contentSize, NO, 0.0);
[[self.tableView layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
/**
开始截图
*/
- (void)beginCapture
{
//tableview的截图
UIImage *image = [self captureImage];
CoinDetailShareViewController *vc = [[CoinDetailShareViewController alloc] init];
vc.image = image;
vc.coinName = self.coinName;
[self presentViewController:vc animated:YES completion:^{
//将控件大小设置回去
[self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.right.offset(0);
make.bottom.equalTo(self.collectLabel.mas_top);
make.top.equalTo(self.customNavBar.mas_bottom);
}];
}];
}
网友评论