简体   繁体   English

iPhone将UIListView渲染(或捕获)到图像中

[英]iPhone Rendering (or capturing) UIListView into an image

I'd like to render a basic UIListView into an image I can save. 我想将基本的UIListView渲染为可以保存的图像。 I found lots of code here for rendering a UIView to an image (for iphone/iPod Touch). 我在这里找到了大量用于将UIView呈现到图像的代码(适用于iPhone / iPod Touch)。 In fact, the code below is what I pulled from other posts and am using now. 实际上,下面的代码是我从其他帖子中提取的,现在正在使用。 Problem is it doesn't draw anything from the UIListView which isn't visible when the method is invoked. 问题是它没有从UIListView绘制任何东西,而在调用该方法时,这是不可见的。

- (void)snapShotAction:(id)sender
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    UIGraphicsBeginImageContext(screenRect.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor blackColor] set];
    CGContextFillRect(ctx, screenRect);

    //[self.view.layer renderInContext:ctx];
    [self.myTableView.layer renderInContext:ctx];

    UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    
    UIImageWriteToSavedPhotosAlbum(screenImage, nil, nil, nil);

}

So I'd like the flexibility of being able to force the entire UIListView to render (including offscreen rows) to an image, but if no one has some insight for making that happen, I guess my alternative is to create an image or graphicscontext and just render a facsimile of the list manually (and any references to sample code for doing that are appreciated). 因此,我希望能够强制整个UIListView渲染(包括屏幕外的行)为图像的灵活性,但如果没有人对此有所了解,我想我的替代方法是创建图像或graphicscontext,只需手动呈现该列表的传真即可(对于这样做的示例代码的任何引用,我们将不胜感激)。

Here's another idea that occurred to me. 这是我想到的另一个主意。 Perhaps I can use a separate UITableView with no bottom or top bars (to maximize horizontal space) then calculate the row heights based on the number of items in the table then use the code above to capture that. 也许我可以使用没有底部或顶部条的单独UITableView(以最大化水平空间),然后根据表中的项目数计算行高,然后使用上面的代码捕获行高。 Of course there will be limits to how many items the list can have before the text size is too small to be usable... 当然,在文本大小过小以致无法使用之前,列表可以包含多少项是有限制的。

The problem lies in the way UIListView works. 问题在于UIListView的工作方式。 In order to save memory, it doesn't render a cell until just before it's going to be displayed, so any UIListViewCell that is off screen may not even be rendered. 为了节省内存,它直到要显示它之前都不会渲染单元格,因此屏幕外的任何UIListViewCell甚至都不会渲染。 Short of digging in and injecting code into UIListView to make it render differently (probably not a good idea), you're going to have to stitch it together by scrolling down the list. 缺少深入挖掘并将代码注入UIListView以使其呈现不同的外观(可能不是一个好主意),您将不得不通过向下滚动列表来将其拼接在一起。

Here's an idea you can try that probably won't work: try changing the bounds of the window and all relevant views to be larger than the screen, large enough to hold the entire UIListView at once. 这是一个您可能无法使用的想法:尝试将窗口和所有相关视图的边界更改为大于屏幕,并使其大小足以一次容纳整个UIListView。 Assuming that you're running on the simulator this shouldn't kill the memory. 假设您正在模拟器上运行,这不应占用内存。 However, I have a feeling that UIListView is smarter than to be fooled by that and bases how much it renders in part on the size of the screen. 但是,我有一种感觉UIListView比被它愚弄还要聪明,它基于屏幕的大小来呈现多少。 But it's probably worth a shot. 但这可能值得一试。

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

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