Iphone 视网膜显示核心图形
我正在使用下面的代码对加载的图像执行一些操作,但我发现在视网膜显示器上显示时会变得模糊Iphone 视网膜显示核心图形,iphone,core-graphics,retina-display,cg,Iphone,Core Graphics,Retina Display,Cg,我正在使用下面的代码对加载的图像执行一些操作,但我发现在视网膜显示器上显示时会变得模糊 - (UIImage*)createImageSection:(UIImage*)image section:(CGRect)section { float originalWidth = image.size.width ; float originalHeight = image.size.height ; int w = originalWidth * section.size.width; int
- (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;
}
我如何更改它以使其与视网膜兼容
提前谢谢你的帮助
最好的,
DVCGImages不考虑设备的视网膜亮度,因此您必须自己考虑 为此,您需要将CoreGraphics例程中使用的所有坐标和大小乘以输入图像的
scale
属性(在视网膜设备上为2.0),以确保以双分辨率进行所有操作
然后您需要更改
resultImage
的初始化以使用initWithCGImage:scale:orientation:
并输入相同的比例因子。这就是视网膜设备以本机分辨率而不是像素倍增分辨率渲染输出的原因。感谢您的回答,帮助了我!无论如何,为了更清楚,OP的代码应该更改如下:
- (UIImage*)createImageSection:(UIImage*)image section:(CGRect)section
{
CGFloat scale = [[UIScreen mainScreen] scale]; ////// ADD
float originalWidth = image.size.width ;
float originalHeight = image.size.height ;
int w = originalWidth * section.size.width *scale; ////// CHANGE
int h = originalHeight * section.size.height *scale; ////// CHANGE
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);
CGContextScaleCTM(UIGraphicsGetCurrentContext(), scale, scale); ////// ADD
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;
}
Swift版本:
let scale = UIScreen.mainScreen().scale
CGContextScaleCTM(UIGraphicsGetCurrentContext(), scale, scale)
如果通过调用
CGContextScaleCTM(UIGraphicsGetCurrentContext(),2,2)
更改上下文的CTM(当前变换矩阵),则无需手动倍增坐标,只需绘制与非视网膜图像相同的坐标。抱歉,注释太快,编辑太晚。如果你这样做,你不需要考虑自己缩放所有坐标:CGFloat scale=[[UIScreen mainScreen]scale];CGContextScaleCTM(UIGraphicsGetCurrentContext(),缩放,缩放)代码>太多了,是的。很好的解决方案。如果你只画了图像的一部分(一个子矩形),你仍然需要手动调整比例。