0

I am trying to export a tableview as an image. I'd like control over the size of that image.

The problem is that the image is as big as the frame of the tableview, which on the iPhone in my case is 320x480 (or some other height). I want it to be bigger. Actually I want to have control over the size of the exported image. For example I don't want to export image 320x480. I need some sort of scale factor. For example I would like to have 2 times bigger image with 2 times more pixels... I hope I explained it well. Here is some code I use for the image creation.

- (UIImageView*)renderTableViewAsImageView {

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView cellForRowAtIndexPath:indexPath].hidden = YES;
    self.invoiceExportTitle.textColor = [UIColor blackColor];

    CGFloat scaleFactor = 1.0;
    CGSize pageSize = CGSizeMake(self.tableView.contentSize.width*scaleFactor, self.tableView.contentSize.height*scaleFactor);

    UIGraphicsBeginImageContext(pageSize);

    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
    [self.tableView.layer renderInContext:UIGraphicsGetCurrentContext()];

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

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

    UIGraphicsEndImageContext();

    [self.tableView cellForRowAtIndexPath:indexPath].hidden = NO;
    self.invoiceExportTitle.textColor = [UIColor clearColor];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];

    return imageView;
}
Clay Bridges
  • 11,602
  • 10
  • 68
  • 118
stellz
  • 130
  • 1
  • 8
  • Would it work just to resize the UIImage?, e.g.: http://stackoverflow.com/questions/2658738/the-simplest-way-to-resize-an-uiimage – Clay Bridges Feb 21 '14 at 15:04

1 Answers1

0

When you have your UIImage, you can draw it in a scaled version with

[image drawInRect:CGRectMake(0,0,width, height)];

in your graphics context. Of course, the pixels will be interpolated, but your base image is only 320x480, so the quality will stay like that.

TheEye
  • 9,280
  • 2
  • 42
  • 58
  • Actually I need more quality on the base image. I don't want to resize the generated image, but I want to originally generate bigger image – stellz Feb 24 '14 at 09:01
  • That's not possible - what you do is draw the screen content to an image, and that can only have the screen resolution. If you want to have higher quality, you have to define all the drawn objects yourself in a kind of vector-based array and draw these objects into an image. – TheEye Feb 24 '14 at 09:14
  • hmm, I see, 10x for the explanation – stellz Feb 24 '14 at 10:29