解决方法是在绘制标签之前将标签的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];
}
} ];
}
这可以工作,但是如果你想捕获一个UITableView,单独改变每个图层是一种痛苦。没有办法改变它的所有子视图递归? – charliehorse55 2012-07-28 22:20:26
那么,没有我知道的单一命令,但注意扩展的代码会缓解并完成大部分工作。如果您没有将imageViews设置为2x图像(对于非视网膜显示器来说工作正常,在渲染时使系统工作更多一点,您将不得不做一些特别的事情。 – 2012-07-31 00:45:34