2015-05-10 48 views
2

我使用-drawViewHierarchyInRect:afterScreenUpdates:繪製viewUIImage,但似乎不管我做了什麼,結果始終是一個非常低的分辨率,因爲圖像似乎需要與原始視圖大小相同?UIView drawViewHierarchyInRect給出像素化結果?

UIView是屏幕的大小(本例中爲375 x 667),內容比例因子爲2.0f。

但是,當我使用-drawViewHierarchyInRect:afterScreenUpdates:view繪製到image上時,即使內容的分辨率高得多,也會調整(可怕)該分辨率。

我使用的是UIGraphicsBeginImageContextWithOptions,其比例爲0.0來創建image,所以我認爲它應該很好地繪製,但沒有去。你如何繪製視圖層次結構而不會丟失細節?

編輯: 下面是一些示例代碼(來自下面的答案,它給出了相同的結果)。

UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0); 
[self.view drawViewHierarchyInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) afterScreenUpdates:NO]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageView *imgView = [[UIImageView alloc] initWithImage:image]; 
imgView.frame = self.view.bounds; 
[self.view addSubview:imgView]; 
[self.view bringSubviewToFront:imgView]; 
+0

你可以粘貼你的代碼在你的問題? – Tobias

回答

0

此代碼的工作完美:試圖繪製的UIView層次,其中包括高清晰度的圖像時,它給了我可怕的按比例縮小的效果。它截取當前view的屏幕截圖並將其放到新創建的UIImageView中。請檢查

UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0); 
[self.view drawViewHierarchyInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) afterScreenUpdates:NO]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageView *imgView = [[UIImageView alloc] initWithImage:image]; 
imgView.frame = self.view.bounds; 
[self.view addSubview:imgView]; 
[self.view bringSubviewToFront:imgView]; 
+0

這實際上正是我所做的。使用包含高分辨率圖像的UIView運行這個功能時,會出現非常小的縮放結果,因爲它試圖將其適合屏幕分辨率(沒有縮放比例),而屏幕分辨率要低得多。 – Rob

+0

你能分享更多細節嗎?我無法在我的機器上重現像素化圖像。也許你只有在特定設備或只有模擬器纔有這種體驗? – Azat

+0

我不太確定如何解釋它。我試過iOS7和iOS8的多種設備。例如,如果我有一個帶有高分辨率圖像的UIImageView的UIView,那麼使用drawViewHierarchyInRect,圖像縮小非常糟糕。看起來這樣做是爲了匹配UIView的大小(沒有縮放?!),儘管它顯示時它是完美的質量。我確信我做錯了什麼,但不能把我的手指放在上面。 – Rob