我有这个代码,用于呈现内部带有动态文本的自定义滑动手柄。 问题是我无法将其扩展到视网膜分辨率。它适用于所有设备,尺寸合适,但视网膜分辨率较低。 如何更改此代码以使最终图像分辨率加倍?
-(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;
}
答案 0 :(得分:1)
更改创建上下文:
CGFloat scale = [[UIScreen mainScreen] scale]
CGContextRef context = CGBitmapContextCreate(NULL, w * scale, h * scale, 8, 0, colorSpace,kCGImageAlphaPremultipliedFirst);
答案 1 :(得分:1)
您可以使用屏幕比例从当前上下文中获取Retina图像。
这是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!
}