用于视网膜显示的CoreGraphics [英] CoreGraphics for retina display
问题描述
我正在使用以下代码对加载的图像执行一些操作,但发现在视网膜显示屏上时,显示屏变得模糊
I am using the following code to perform some manipulations on the image that I loaded, but I find that the display becomes blurry when it is on the retina display
- (UIImage*)createImageSection:(UIImage*)image section:(CGRect)section
{
float originalWidth = image.size.width ;
float originalHeight = image.size.height ;
int w = originalWidth * section.size.width;
int h = originalHeight * section.size.height;
CGContextRef ctx = CGBitmapContextCreate(nil, w, h, 8, w * 8,CGImageGetColorSpace([image CGImage]), kCGImageAlphaPremultipliedLast);
CGContextClearRect(ctx, CGRectMake(0, 0, originalWidth * section.size.width, originalHeight * section.size.height)); // w + h before
CGContextTranslateCTM(ctx, (float)-originalWidth * section.origin.x, (float)-originalHeight * section.origin.y);
CGContextDrawImage(ctx, CGRectMake(0, 0, originalWidth, originalHeight), [image CGImage]);
CGImageRef cgImage = CGBitmapContextCreateImage(ctx);
UIImage* resultImage = [[UIImage alloc] initWithCGImage:cgImage];
CGContextRelease(ctx);
CGImageRelease(cgImage);
return resultImage;
}
如何更改它以使其与视网膜兼容.
How do I change this to make it retina compatible.
预先感谢您的帮助
最好, DV
推荐答案
CGImage不会考虑设备的视网膜外观,因此您必须自己这样做.
CGImages don't take into account the Retina-ness of your device, so you have to do so yourself.
为此,您需要将CoreGraphics例程中使用的所有坐标和尺寸乘以输入图像的scale
属性(在Retina设备上为2.0),以确保以双分辨率进行所有操作
To do that, you need to multiply all of your coordinates and sizes that use in CoreGraphics routines by the input image's scale
property (which will be 2.0 on Retina devices), to ensure you do all your manipulation at double resolution.
然后,您需要更改resultImage
的初始化以使用initWithCGImage:scale:orientation:
并输入相同的比例因子.这就是使Retina设备以原始分辨率而不是像素加倍分辨率呈现输出的原因.
Then you need to change the initialization of resultImage
to use initWithCGImage:scale:orientation:
and input the same scale factor. This is what makes Retina devices render the output at native resolution rather than pixel-doubled resolution.
这篇关于用于视网膜显示的CoreGraphics的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!