【问题标题】:How to force -drawViewHierarchyInRect:afterScreenUpdates: to take snapshots at @2x resolution?如何强制 -drawViewHierarchyInRect:afterScreenUpdates: 以@2x 分辨率拍摄快照?
【发布时间】:2014-02-04 10:57:40
【问题描述】:

-drawViewHierarchyInRect:afterScreenUpdates: 是 iOS 7 中的 fast way,用于获取视图层次结构的快照。

它拍摄快照,但分辨率为@1x。在 iPhone 5S 上,这些快照看起来像素化且模糊。我创建快照的视图没有被转换。

我不想模糊它,我想要在屏幕上看到的高质量。 这是我如何捕捉它的:

UIGraphicsBeginImageContext(self.bounds.size);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

我也试过了:

UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, self.contentScaleFactor);

仍然以低@1x 质量呈现。

是否有另一种方法来配置图像上下文,使其达到@2x 分辨率?

【问题讨论】:

    标签: ios iphone ipad ios7 core-graphics


    【解决方案1】:

    正确的应该是:

    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0f);
    

    【讨论】:

    • 为什么比例因子为 0?
    • 参见documentation of UIGraphicsBeginImageContextWithOptions :“应用于位图的比例因子。如果指定值为 0.0,则比例因子设置为设备主屏幕的比例因子。”
    • 是的,文档清楚地说明了这一点。除非您需要特定的比例因子,否则请始终使用 0.0。
    【解决方案2】:

    好的,所以问题是 self.contentScaleFactor 在视网膜显示设备上返回了错误的值。应该是 2 的地方是 1。

    这可行,但当然它不太理想,因为它没有非视网膜设备的后备。

    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 2);
    

    【讨论】:

    • 我会看看@graver 的答案...使用 0.0 作为比例因子应该适合您的需求。
    • 是的,如果您正在寻找设备分辨率,@graver 的回答是正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    相关资源
    最近更新 更多