2012-02-25 34 views
11

我需要將圖像繪製到CALayer中,因爲我需要對其執行各種效果,動畫和濾鏡。當我做簡單的CGContext繪圖時,無論我做什麼,它總是被繪製爲像素化... 什麼是正確的方式來繪製在視網膜上下文?Retina中的CGContextDrawImage繪製圖像像素化?

這是我現在在做什麼:

CGImageRef plateImage = [[UIImage imageNamed:@"someImage"] CGImage]; 
CGFloat width = CGImageGetWidth(plateImage), height = CGImageGetHeight(plateImage); 
CGFloat scale = [[UIScreen mainScreen] scale]; 

NSLog(@"Scale: %f\nWidth: %f\nHeight: %f", scale, width, height); 
CGContextTranslateCTM(_context, 0, height/scale); 
CGContextScaleCTM(_context, 1.0, -1.0); 

CGContextDrawImage(_context, CGRectMake(0, 0, width/scale, height/scale), plateImage); 

回答

18

,需要相應地設置圖層的內容規模。

myLayer.contentsScale = [UIScreen mainScreen].scale 
+0

Aaaaaa!我在CG文檔中無處不在 - 一分鐘沒有想到它是一個圖層問題!謝謝! – shein 2012-02-25 17:02:12

+0

令人驚歎。謝謝。 – darkheartfelt 2014-01-29 19:18:38

23

我有同樣的問題,但解決方案似乎沒有工作。

UIGraphicsBeginImageContext()竟然是我的問題。我在這裏發佈我的解決方案,爲未來的用戶提供同樣的問題。

從iOS的4.0你應該使用:的

UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); 

代替

UIGraphicsBeginImageContext(size); 

如果你不想像素化圖像。

+0

謝謝!我不能說我明白爲什麼這會起作用,但它確實如此。 – anonymouse 2013-07-05 22:23:21

+2

這應該是正確的答案! – Fydo 2014-07-28 12:54:08

+2

值得注意的是,我將這個結果與接受的答案結合起來得到了最好的結果: 'UIGraphicsBeginImageContextWithOptions(imageSize,NO,[UIScreen mainScreen] .scale);' – bplattenburg 2015-10-02 18:21:55