[英]CGContextDrawImage in Retina draws image pixelated?
我需要將圖像繪制到 CALayer 中,因為我需要在其上執行各種效果、動畫和過濾器。 當我在 CGContext 中進行簡單的繪制時,無論我做什么,它總是會被像素化……在視網膜中繪制上下文的正確方法是什么?
這就是我現在正在做的事情:
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);
我遇到了同樣的問題,但解決方案似乎不起作用。
UIGraphicsBeginImageContext()
原來是導致我的問題。 我在這里為有同樣問題的未來用戶發布我的解決方案。
從 iOS 4.0 你應該使用:
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
代替
UIGraphicsBeginImageContext(size);
如果您不想要像素化圖像。
您需要適當地設置圖層的內容比例。
myLayer.contentsScale = [UIScreen mainScreen].scale
更新了@Tieme 的 Swift 4 解決方案
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
代替
UIGraphicsBeginImageContext(size)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.