【发布时间】:2016-10-10 13:23:49
【问题描述】:
我正在尝试创建一个实用程序函数,该函数允许我从 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;,它将从表格视图的底部而不是顶部移除更多的高度。
更新 - 我现在有了following image,这几乎是我所需要的,但在图像底部留下了一个黑条,我似乎无法正确删除它。它的高度等于topOffset+bottomOffset。
关于我应该如何解决这个问题的任何建议?
【问题讨论】:
-
我的建议是复制这个 tableView 实例并删除你不想要的额外内容,然后渲染它。
-
老实说,我真的希望尽可能避免这种情况
-
好的,你可以看看 UIRectClip developer.apple.com/library/ios/documentation/UIKit/Reference/…
-
我使用了您的示例和
CGContextClipToRect来更接近解决方案,但现在我的图像底部留下了一个大的黑色部分。你有什么建议吗?
标签: ios objective-c uitableview uiimage