【发布时间】: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;
}
【问题讨论】:
-
仅调整 UIImage 的大小是否有效?例如:stackoverflow.com/questions/2658738/…
标签: ios iphone objective-c uitableview uiimageview