簡體   English   中英

渲染視圖,縮放@ 2x,renderInContext,iPhone

[英]Rendering views, scaled @2x, renderInContext, iPhone

我有一個包含圖形的視圖(稱為outPutView),如uIImageViews和標簽。 我需要渲染outPutView的圖像及其子視圖。 我正在使用renderInContext:UIGraphicsGetCurrentContext()來做到這一點。 工作正常,但我需要縮放視圖。 我在outPutView上使用轉換。 這會成功縮放視圖及其子視圖,但轉換不會呈現。 雖然視圖在屏幕上縮放。 最終渲染以原始大小顯示視圖,而渲染上下文為目標大小(此處為@ 2x iPhone視圖大小)。

謝謝閱讀!!

[outPutView setTransform:CGAffineTransformMake(2, 0, 0, 2, 0, 0)];
CGSize renderSize = CGSizeMake(self.view.bounds.size.width*2, self.view.bounds.size.height*2);
UIGraphicsBeginImageContext(renderSize);
[[outPutView layer] renderInContext:UIGraphicsGetCurrentContext()];
renderedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

我剛剛完成了這項工作,盡管方向相反(縮小)。 以下是相關代碼的摘要:

// destination size is half of self (self is a UIView)
float rescale = 0.5;
CGSize resize = CGSizeMake(self.width * rescale, self.height * rescale);

// make the destination context
UIGraphicsBeginImageContextWithOptions(resize, YES, 0);

// apply the scale to dest context
CGContextScaleCTM(UIGraphicsGetCurrentContext(), rescale, rescale);

// render self into dest context
[self.layer renderInContext:UIGraphicsGetCurrentContext()];

// grab the resulting UIImage
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

要向上擴展而不是向下擴展, rescale = 2應該沒問題。

我通過重新排序我的觀點來解決這個問題。 實際上在輸出視圖之間添加另一個視圖:從中獲取渲染上下文的視圖,以及通過transform縮放的視圖。 它有效,但我不知道為什么在這一點上。 對此的任何想法將不勝感激。 謝謝閱讀。

暫無
暫無

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

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