How to Render a Whole Uitableview as an Uiimage in iOS

creating a UIImage from a UITableView

i just Create a DEMO for you and hope its helps you you can capture table image like this way:-

-(IBAction)savebutn:(id)sender
{
[tbl reloadData];

CGRect frame = tbl.frame;
frame.size.height = tbl.contentSize.height;
tbl.frame = frame;

UIGraphicsBeginImageContext(tbl.bounds.size);
[tbl.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(saveImage);
NSFileManager *fileMan = [NSFileManager defaultManager];

NSString *fileName = [NSString stringWithFormat:@"%d.png",1];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
[fileMan createFileAtPath:pdfFileName contents:imageData attributes:nil];



}

This is a Screen Shot of capture image:-

Sample Image

capture image contain full of table cell top to bottom please download the demo of it:-

http://www.sendspace.com/file/w48sod

How do I capture UIImage of complete contents of UITableView / UIScrollView and make it work on an ios device

It turns out that using the [UIImage ..] resizableImageWithCapInsets] method and applying it to the cell backgroundView and adjusting the frame causes all sorts of funky badness to happen on actual devices but not the simulator.

Reverting back to the deprecated [UIImage ..] stretchableImageWithLeftCapWidth: topCapHeight:] method solved it.

Capture UITableView as Image - With Scrolling

capture whole tableview as a image

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(tableView.contentSize.width, tableView.contentSize.height),false, 0.0)

let context = UIGraphicsGetCurrentContext()

let previousFrame = tableView.frame

tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.contentSize.width, tableView.contentSize.height);

tableView.layer.renderInContext(context!)

tableView.frame = previousFrame

let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();

imageView.image = image;

capture the screenshot of tableview in a scrolled position

    let contentOffset = tableView.contentOffset

UIGraphicsBeginImageContextWithOptions(tableView.bounds.size, true, 1)

let context = UIGraphicsGetCurrentContext()

CGContextTranslateCTM(context, 0, -contentOffset.y)

tableView.layer.renderInContext(context!)

let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

imageView.image = image;

Create UIImage from UITableView with top and bottom offset

Ok so I have fairly reasonable solution. One of the major roadblocks I hit when doing this was I didn't realize that Photos centre crops images in the preview. You have to actually zoom out to see the full image... that made we waste way too much time.

UIImage from UITableView with top and bottom offset

+ (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;
}

Also I need similar functionality for creating a UIImage from a UIView with similar top and bottom offset so I've added that function here as well in case you need it.

UIImage from UIView with top and bottom offset

+ (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;
}

You'll notice this method is much simpler because you do't have to modify the tableview frame to match the content size before taking the "screenshot". The central chunk of code can be factored out but I left it in both solutions for easy copy pasting.

Make UIImageView fill the whole UITableViewCell

I think what you want is : backgroundView property of UITableViewCell

You can do it like this :

-(void)  tableView:(UITableView*)tableView 
willDisplayCell:(UITableViewCell*)cell
forRowAtIndexPath:(NSIndexPath*)indexPath;
{
static UIImage* bgImage = nil;
if (bgImage == nil) {
bgImage = [[UIImage imageNamed:@"myimage.png"] retain];
}
cell.backgroundView = [[[UIImageView alloc] initWithImage:bgImage] autorelease];
}

Or Simply Use :

((UIImageView*)cell.backgroundView).image = [UIImage imageNamed:@"customBackground"];
((UIImageView*)cell.selectedBackgroundView).image = [UIImage imageNamed:@"selectedBackground"];

How to save the whole scrollable UITableView to PNG

Here are some Swift extensions files you can add to your project to make screenshots:

The new code would be:

let image = tableView.screenshot
let data = UIImagePNGRepresentation(image)
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let imageURL = documentsURL.URLByAppendingPathComponent("cached.png")
data!.writeToURL(imageURL, atomically: false)


Related Topics



Leave a reply



Submit