简体   繁体   English

如何拍摄整个视图的iPhone屏幕截图,包括屏幕外的部分?

[英]How to take an iPhone screenshot of entire view including parts off screen?

I have an application that's laid out using an UITableView. 我有一个使用UITableView布局的应用程序。 The interface takes up more than just the screen, so there's some area that can be scrolled to. 界面占用的不仅仅是屏幕,因此可以滚动到某些区域。 I would like some screenshots of the entire view (including the non-visible area) to use for documentation and soliciting feedback from my customer. 我想要一些用于文档和征求客户反馈的整个视图(包括不可见区域)的截图。

Is there programmatic way to get an image of the entire view? 是否有编程方式来获取整个视图的图像? I don't think there would be a way on the device to do this, but maybe there is. 我不认为设备上会有这样的方法,但也许有。

Just pasting some code I used in my app. 只是粘贴我在我的应用程序中使用的一些代码。 It draws a view to an image (potentially scaling). 它绘制了一个图像的视图(可能缩放)。 Maybe you could programmaticaly scroll the table and stitch the images together in the end or something similar: 也许你可以通过programmaticaly滚动表格并将图像拼接在一起或类似的东西:

+ (UIImage *)captureView: (UIView *)view inSize: (CGSize)size
{
    UIGraphicsBeginImageContext(size);
    CGSize viewSize = view.frame.size;
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextScaleCTM( context, size.width/viewSize.width, size.height/viewSize.height);

    [view.layer renderInContext: UIGraphicsGetCurrentContext()];

    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}

Hope it helps 希望能帮助到你

You can actually scroll the tableview. 你实际上可以滚动tableview。 The following code works perfect in my application. 以下代码在我的应用程序中完美运行。 But it may take a white for making the screenshot. 但制作屏幕截图可能需要白色。 You may refer to this to see if anyone improved. 你可以参考这个看看是否有人改进。

//viewController is your UITableViewController
UIGraphicsBeginImageContext(viewController.tableView.contentSize);
[viewController.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
[viewController.tableView.layer renderInContext:UIGraphicsGetCurrentContext()];

int rows = [viewController.tableView numberOfRowsInSection:0];
int numberofRowsInView = 7;
for (int i =0; i < rows/numberofRowsInView; i++) {
    [viewController.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(i+1)*numberofRowsInView inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    [viewController.tableView.layer renderInContext:UIGraphicsGetCurrentContext()];

}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Take a screenshot of the whole table view and not just visible cells. 截取整个表格视图的截图,而不仅仅是可见的单元格。 This approach doesn't require scrolling the table view and taking many screenshots and putting them together. 这种方法不需要滚动表格视图并拍摄许多屏幕截图并将它们放在一起。

Works well with table view footer and header. 适用于表格视图页脚和标题。

CGRect frame = _tableView.frame;
frame.size.height = _tableView.contentSize.height;//the most important line
_tableView.frame = frame;

UIGraphicsBeginImageContext(_tableView.bounds.size);
[_tableView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData * data = UIImagePNGRepresentation(image);

With a table view on the iphone, there is no "extra" area to scroll to. 在iphone上有一个表格视图,没有“额外”区域可以滚动到。 The table view is as big as the screen, and as new cells are scrolled into view, they're created on demand, while old cells that are scrolled off screen are deallocated. 表格视图与屏幕一样大,当新单元格滚动到视图中时,它们是按需创建的,而在屏幕上滚动的旧单元格将被取消分配。

As Jasarien said , a UITableView has no off screen elements. 正如Jasarien所说 ,UITableView没有屏幕外元素。 It's just a stage illusion that creates the appearance of single long element. 这只是一个阶段错觉,创造了单个长元素的外观。

If you want to create an image of long (ie offscreen) table, you should take screenshots of the table at different scroll positions and then Photoshop/GIMP/insert-graphics-app the screenshots into one long graphic. 如果你想创建一个长(即屏幕外)表的图像,你应该在不同的滚动位置截取表格 ,然后Photoshop / GIMP / insert-graphics-app将截图设置成一个长图形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM