随着分享内容的需求的不断完善,将当前页面的内容分享出去变成了产品经理的需求,之前常规的的分享显然已经不能满足,纷纷开始定制分享的图片内容。尤其以区块链相关的App特别多,作为运营拉新的手段,需要及时的将行情数据、免费赠送的token信息发送出去,激活用户的热情。通常看到的操作方式如下即:分享的图片需要上下拼接内容,上半部分显示为App名称、简介, 下半部分为注册二维码。但是行情页面的分享已经超过了常规的屏幕可见内容, 需要将tableview未显示的内容展示出来,无疑增加了难度。
下面将常用的几种View生成图片的方法做个归类:
1.将传入的View生成图片:
+ (UIImage *)makeImageWithView:(UIView *)view{
CGSize s = view.bounds.size;
UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
2.将传入的View生成图片,可以显示layer(分享分时图、k线)
+ (UIImage *)imageFromView:(UIView *)view{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
3.将tableview 生成图片
+ (UIImage *)getTableViewimage:(UITableView *)tableview{
UIImage* viewImage = nil;
UIScrollView *scrollView = tableview;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(scrollView.contentSize.width, scrollView.contentSize.height), NO, 0.0);
{
CGPoint savedContentOffset = scrollView.contentOffset;
CGRect savedFrame = scrollView.frame;
scrollView.contentOffset = CGPointZero;
scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
[scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
viewImage = UIGraphicsGetImageFromCurrentImageContext();
scrollView.contentOffset = savedContentOffset;
scrollView.frame = savedFrame;
}
UIGraphicsEndImageContext();
if (viewImage != nil) {
return viewImage;
}
return nil;
}
4.图片拼接
+ (UIImage *)addSlaveImage:(UIImage *)slaveImage toMasterImage:(UIImage *)masterImage {
CGSize size;
size.width = masterImage.size.width;
size.height = masterImage.size.height + slaveImage.size.height;
UIGraphicsBeginImageContextWithOptions(size, YES, 0.0);
[masterImage drawInRect:CGRectMake(0, 0, masterImage.size.width, masterImage.size.height)];
[slaveImage drawInRect:CGRectMake(0, masterImage.size.height, masterImage.size.width, slaveImage.size.height)];
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
}