用于视网膜显示的 CoreGraphics
我正在使用以下代码对加载的图像执行一些操作,但我发现在视网膜显示屏上时显示变得模糊,
- (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;
}
如何更改它以使其与视网膜兼容。
预先感谢您的帮助
最好, DV
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.
Thanks in advance for your help
Best,
DV
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
CGImages 不考虑您设备的视网膜特性,因此您必须自己这样做。
为此,您需要将 CoreGraphics 例程中使用的所有坐标和大小乘以输入图像的
scale
属性(在 Retina 设备上为 2.0),以确保您在双分辨率。然后,您需要更改
resultImage
的初始化以使用initWithCGImage:scale:orientation:
并输入相同的比例因子。这就是使 Retina 设备以原始分辨率而不是像素加倍分辨率渲染输出的原因。CGImages don't take into account the Retina-ness of your device, so you have to do so yourself.
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.Then you need to change the initialization of
resultImage
to useinitWithCGImage: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.感谢您的回答,对我有帮助!无论如何,为了更清楚,OP的代码应该像这样改变:
Thanks for the answer, helped me! Anyway to be clearer, the code by OP should be changed like this:
斯威夫特版本:
Swift version :