UIView的Uiimage:高于屏幕分辨率?
我有一个UIView,我通过典型的UIGraphicsBeginImageContextWithOptions方法呈现给UIImage,使用2.0的缩放比例,因此图像输出将始终是屏幕上显示内容的“视网膜显示”版本,无论用户的实际屏幕分辨率。UIView的Uiimage:高于屏幕分辨率?
我渲染的UIView包含图像和文本(UIImages和UILabels)。图像以全分辨率显示在呈现的UIImage中,并且看起来很棒。但是UILabel似乎已经以1.0的比例被栅格化,然后被升级到2.0,导致文本模糊。
有没有什么我做错了,或者有什么方法让文本在更高的级别上呈现出漂亮和清晰?或者有什么方法可以做到这一点,而不是使用UIGraphicsBeginImageContextWithOptions的缩放参数来获得更好的结果?谢谢!
解决方法是在绘制标签之前将标签的contentsScale更改为2,然后立即将其设置回来。我只是编写了一个项目来验证它,它的工作很好,在正常的视网膜电话(模拟器)中制作了一个2倍的图像。 [如果你有一个公共的地方,我可以把它让我知道。]
编辑:扩展的代码走子视图和任何容器UIViews到设置/取消规模
- (IBAction)snapShot:(id)sender
{
[self changeScaleforView:snapView scale:2];
UIGraphicsBeginImageContextWithOptions(snapView.bounds.size, snapView.opaque, 2);
[snapView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageDisplay.image = img; // contentsScale
imageDisplay.contentMode = UIViewContentModeScaleAspectFit;
[self changeScaleforView:snapView scale:1];
}
- (void)changeScaleforView:(UIView *)aView scale:(CGFloat)scale
{
[aView.subviews enumerateObjectsUsingBlock:^void(UIView *v, NSUInteger idx, BOOL *stop)
{
if([v isKindOfClass:[UILabel class]]) {
v.layer.contentsScale = scale;
} else
if([v isKindOfClass:[UIImageView class]]) {
// labels and images
// v.layer.contentsScale = scale; won't work
// if the image is not "@2x", you could subclass UIImageView and set the name of the @2x
// on it as a property, then here you would set this imageNamed as the image, then undo it later
} else
if([v isMemberOfClass:[UIView class]]) {
// container view
[self changeScaleforView:v scale:scale];
}
} ];
}
尝试渲染到与双大小的图像,然后创建缩放的图像:
UIGraphicsBeginImageContextWithOptions(size, NO, 1.0);
// Do stuff
UImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
newImage=[UIImage imageWithCGImage:[newImage CGImage] scale:2.0 orientation:UIImageOrientationUp];
其中: 尺寸= realSize *规模;
不知道我理解你'再说一遍。我没有和我一起工作的CGImage,我有一个UIView,然后我将它渲染成一个UIImage ... – DanM 2011-03-28 12:19:17
已编辑。它应该做你想做的。 – ssteinberg 2011-03-28 12:40:17
是不是只是按比例*所有* up?那么现在文本*和*图片都会模糊不清了? – DanM 2011-03-29 14:35:31
我一直在与textview的上下文中的许多相同的古怪与PDF渲染挣扎。我发现构成视图的CALayer对象上有一些记录的属性。也许设置相关(子)层的栅格化比例有助于。
这可以工作,但是如果你想捕获一个UITableView,单独改变每个图层是一种痛苦。没有办法改变它的所有子视图递归? – charliehorse55 2012-07-28 22:20:26
那么,没有我知道的单一命令,但注意扩展的代码会缓解并完成大部分工作。如果您没有将imageViews设置为2x图像(对于非视网膜显示器来说工作正常,在渲染时使系统工作更多一点,您将不得不做一些特别的事情。 – 2012-07-31 00:45:34