2012-09-20 57 views
2

我有一個視圖(稱爲outPutView),其中包含圖形,如uIImageViews和標籤。我需要渲染outPutView的圖像和它的子視圖。我正在使用renderInContext:UIGraphicsGetCurrentContext()來這樣做。工作正常,除了我需要擴大意見。我正在使用outPutView上的轉換。這成功地擴展了視圖和它的子視圖,但是轉換不會呈現。而視圖在屏幕上縮放。最終呈現器將以原始大小顯示視圖,而呈現上下文處於目標大小(此處爲@ 2X iPhone視圖大小)。渲染視圖,縮放@ 2x,renderInContext,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(); 
+0

懷疑你必須在沒有變換的情況下將其渲染到圖像,然後使用'drawInRect:'縮放尺寸將該圖像繪製到另一個圖像中。雖然如果你想出一個辦法來做到這一點,我都是耳朵!兩步探戈浪費記憶,併爲大視野造成麻煩。 – sobri

+0

@sobri閱讀下面的答案。 – Mrwolfy

回答

1

我通過重新排序我的意見解決了這個問題。實際上在輸出視圖之間添加另一個視圖:渲染的上下文從中取出的視圖以及通過變換縮放的視圖。它的工作,但我不知道爲什麼在這一點上。任何想法都將不勝感激。謝謝閱讀。

+0

有趣。所以如果將變換應用於子視圖,那麼它應用於'renderInContext:'?新奇。雖然它能起作用,但它起作用。這避免了必須將變換應用到目標上下文,在許多情況下這將是不受歡迎的。 – sobri

9

我剛剛做了這項工作,雖然在相反的方向(縮小)。下面是相關的代碼內容摘要:

// 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

+0

謝謝,我會試試! – Mrwolfy