簡體   English   中英

從UITableView縮放后的UIImageView

[英]Scaled UIImageView from UITableView

我正在嘗試將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時,可以使用

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

在您的圖形環境中。 當然,將對像素進行插值,但是您的基本圖像僅為320x480,因此質量將保持這樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM