tags:

views:

235

answers:

1

I'd like to render a basic UIListView into an image I can save. I found lots of code here for rendering a UIView to an image (for 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.

- (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).

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. Of course there will be limits to how many items the list can have before the text size is too small to be usable...

+1  A: 

The problem lies in the way UIListView works. 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. 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.

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. 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. But it's probably worth a shot.

Gordon Worley
That was also a thought but I suspect you're right as to why it won't likely work, so I don't think I'll waste the time. My list is essentially just lines of text (a few rows may have an image) I'm planning on just drawing the list into an image context. Since the list may be taller than the overall height of the screen, I'll have to divide the max height by number of items and come up with a bounding box for each row before starting to position and draw.
wkw