Ios 视网膜的CGC分级

Ios 视网膜的CGC分级,ios,objective-c,core-graphics,Ios,Objective C,Core Graphics,我有这段代码,它呈现了一个自定义的幻灯片控制柄,里面有一个动态文本。 问题是我无法将其缩放到视网膜分辨率。它适用于所有设备,尺寸合适,但在视网膜上分辨率较低。 如何将此代码更改为端图像分辨率的两倍 -(UIImage *)addText:(UIImage *)img{ int w = img.size.width; int h = img.size.height; CGColorSpaceRef colorSpace = CGColorSpaceCreateDevi

我有这段代码,它呈现了一个自定义的幻灯片控制柄,里面有一个动态文本。 问题是我无法将其缩放到视网膜分辨率。它适用于所有设备,尺寸合适,但在视网膜上分辨率较低。 如何将此代码更改为端图像分辨率的两倍

-(UIImage *)addText:(UIImage *)img{
    int w = img.size.width;
    int h = img.size.height;



    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 0, colorSpace,kCGImageAlphaPremultipliedFirst);
    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);

    char* text= (char *)[[NSString stringWithFormat:@"$%d", (int)betSlider.value * 5] cStringUsingEncoding:NSASCIIStringEncoding];
    CGContextSetShouldAntialias(context, true);
    CGContextSelectFont(context, "TimesNewRomanPS-BoldMT",mainFrame.size.width/22, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextSetRGBFillColor(context,255,255,255, 1);
    CGContextShowTextAtPoint(context,(img.size.width-(strlen(text)*strlen(text)*(img.size.width/27)))/2.5,img.size.width/2.5,text, strlen(text));
    CGImageRef imgCombined = CGBitmapContextCreateImage(context);


    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    UIImage *retImage = [UIImage imageWithCGImage:imgCombined];
    CGImageRelease(imgCombined);

    return retImage;
}

更改创建上下文,如下所示:

CGFloat scale = [[UIScreen mainScreen] scale]
CGContextRef context = CGBitmapContextCreate(NULL, w * scale, h * scale, 8, 0, colorSpace,kCGImageAlphaPremultipliedFirst);

您可以使用屏幕比例从当前上下文中获取视网膜图像

以下是Swift的解决方案。 Swift 3

func getImageFromContext() -> UIImage{

    UIGraphicsBeginImageContextWithOptions(self.frame.size, false, UIScreen.main.scale)
    self.drawHierarchy(in: self.bounds, afterScreenUpdates: true)
    self.layer.render(in: UIGraphicsGetCurrentContext()!)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return image!
}

我已经试过了。这会使图像大小加倍,而不是所需的分辨率。结果表明,我将图像缩小了两倍是一个愚蠢的错误。img的分辨率是多少?目标图像大小是多少?我使用的是
UIGraphicsBeginImageContext
,甚至没有看到
UIGraphicsBeginImageContextWithOptions
。我是实际上使用的是Xamarin.iOS(我们有相同的方法)。@Matthew:你有方法吗?是的: