【问题标题】:Scaled UIImageView from UITableView从 UITableView 缩放 UIImageView
【发布时间】:2014-02-21 14:45:54
【问题描述】:

我正在尝试将 tableview 导出为图像。我想控制该图像的大小。

问题是图像是 与 tableview 的框架一样大,在我的 iPhone 上是 320x480(或 其他一些高度)。我希望它更大。其实我想控制 超过导出图像的大小。例如我不想导出图像 320x480。我需要某种比例因子。例如我想要 2 图像大两倍,像素多 2 倍……我希望我能解释清楚。 这是我用于创建图像的一些代码。

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

【问题讨论】:

标签: ios iphone objective-c uitableview uiimageview


【解决方案1】:

当你有你的 UIImage 后,你可以用

将它绘制成一个缩放版本

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

在您的图形上下文中。当然,像素会被插值,但你的基础图像只有 320x480,所以质量会保持不变。

【讨论】:

  • 实际上我需要更多的基础图像质量。我不想调整生成的图像的大小,但我想最初生成更大的图像
  • 这是不可能的——你所做的就是将屏幕内容绘制到图像上,并且只能具有屏幕分辨率。如果你想有更高的质量,你必须自己在一种基于矢量的数组中定义所有绘制的对象,并将这些对象绘制成图像。
  • 嗯,我明白了,10 倍的解释
猜你喜欢
  • 1970-01-01
  • 2020-10-01
  • 2012-05-27
  • 1970-01-01
  • 2015-07-12
  • 2018-08-31
  • 2012-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多