[英]Create UIImage from UITableView with top and bottom offset
我正在尝试创建一个实用程序函数,该函数将允许我从UITableView创建UIImage供用户共享。
我遇到的问题是,表的标题视图(如果用户不付费,则是广告)和表的最后一行(内容的实用程序按钮栏)都有多余的内容。
更新 -为此,我具有以下功能:
+ (UIImage *)imageFromTable:(UITableView *)tableView
topOffset:(CGFloat)topOffset
bottomOffset:(CGFloat)bottomOffset {
UIImage *image = nil;
// store the old frame so we can reset the table after the image is created
CGRect oldFrame = tableView.frame;
// set the table frame equal to content size so content is not cut off by screen dimens
[tableView setFrame:CGRectMake(0, 0, tableView.contentSize.width, tableView.contentSize.height)];
// create a rendering frame to use for cropping our image
CGRect renderFrame = CGRectZero;
renderFrame.size.width = tableView.contentSize.width;
renderFrame.size.height = tableView.contentSize.height;
renderFrame.size.height -= bottomOffset;
// generate the image from the custome tableview frame
UIGraphicsBeginImageContextWithOptions(tableView.contentSize, tableView.opaque, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, 0, -topOffset); // move up by top offset to crop top content
CGContextClipToRect(ctx, renderFrame); // clip to render frame to crop bottom content
[tableView.layer renderInContext:ctx];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// TODO: remove black excess at bottom of image
// reset the table with the old frame and return the image
[tableView setFrame:oldFrame];
return image;
}
不幸的是,此方法只能成功处理底部内容的删除。
如果我也做 renderFrame.size.height -= topOffset;
它将从表格视图的底部而不是顶部进一步移除高度。
更新 -我现在有以下图像 ,几乎是我所需要的,但是图像底部有一个黑条,我似乎无法正确删除。 它的高度等于topOffset+bottomOffset
。
关于如何解决此问题的任何建议?
我认为您需要更改框架的origin
以删除顶部的内容, renderFrame.origin.y += topOffset
和renderFrame.size.height -= (topOffset + bottomOffset)
好吧,我有一个相当合理的解决方案。 我这样做时遇到的主要障碍之一是,我没有意识到照片中心会在预览中裁剪图像 。 您实际上必须缩小以查看完整图像……这使我们浪费太多时间。
+ (UIImage *)imageFromTable:(UITableView *)tableView
topOffset:(CGFloat)topOffset
bottomOffset:(CGFloat)bottomOffset {
UIImage *image = nil;
// store the old frame so we can reset the table after the image is created
CGRect oldFrame = tableView.frame;
// set the table frame equal to content size so content is not cut off by screen dimens
[tableView setFrame:CGRectMake(0, 0, tableView.contentSize.width, tableView.contentSize.height)];
// create a cropping frame to use for cropping our image
CGRect cropFrame = CGRectZero;
cropFrame.size.width = tableView.contentSize.width;
cropFrame.size.height = tableView.contentSize.height;
cropFrame.size.height -= topOffset;
cropFrame.size.height -= bottomOffset;
// create image context with tableView content size
// 0.0 uses [UIMain screen].scale
UIGraphicsBeginImageContextWithOptions(cropFrame.size, tableView.opaque, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, 0, -topOffset); // move up by top offset to crop top content
[tableView.layer renderInContext:ctx]; // render our layer
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// reset the table with the old frame and return the image
[tableView setFrame:oldFrame];
return image;
}
另外,我还需要类似的功能来从具有相似的顶部和底部偏移量的UIView创建UIImage,因此我也在此处添加了该功能,以防您需要。
+ (UIImage *)imageFromView:(UIView *)view
topOffset:(CGFloat)topOffset
bottomOffset:(CGFloat)bottomOffset {
UIImage *image = nil;
// create a rendering frame to use for cropping our image
CGRect cropFrame = CGRectZero;
cropFrame.size.width = view.frame.size.width;
cropFrame.size.height = view.frame.size.height;
cropFrame.size.height -= topOffset;
cropFrame.size.height -= bottomOffset;
// create image context with tableView content size
// 0.0 uses [UIMain screen].scale
UIGraphicsBeginImageContextWithOptions(cropFrame.size, view.opaque, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, 0, -topOffset); // move up by top offset to crop top content
[view.layer renderInContext:ctx]; // render our layer
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
您会注意到此方法更加简单,因为在拍摄“截屏”之前不必修改表视图框架以匹配内容大小。 可以分解出中央代码块,但我在两种解决方案中都保留了它们,以便于复制粘贴。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.