性能滚动和视网膜分辨率图像在CALayer

时间:2021-03-24 16:54:15

I have a UIView that is placed as a subview in a UIScrollView. I have several child views made up of images, text, and buttons in the UIView. In order to get decent scrolling performance I set shouldRasterize = YES on the layer in the UIView. This worked great in that performance increased so I have smooth scrolling and doesn't pose an issue since my graphics are static once drawn. However, the problem is that when I set shouldRasterize that the rasterized graphics are blurry and low resolution on a Retina display. Is there a way to have high resolution graphics that are rasterized for performance?

我有一个UIView作为UIScrollView的子view。我有几个子视图由UIView中的图像、文本和按钮组成。为了获得更好的滚动性能,我在UIView的层上设置了shouldRasterize = YES。这在性能上做得很好,因此我有平滑的滚动,并且不会造成问题,因为我的图形一旦绘制就会是静态的。然而,问题是当我设置shouldRasterize的时候,光栅化的图形在视网膜显示屏上是模糊的和低分辨率的。是否有一种方法可以让高分辨率的图形被栅格化以获得性能?

2 个解决方案

#1


120  

Seems I needed to set rasterizationScale to the proper value for the device as follows.

似乎我需要将rasterizationScale设置为设备的适当值,如下所示。

myView.layer.rasterizationScale = [[UIScreen mainScreen] scale];

#2


0  

I had a similar problem-- a rotated UIView with several UIImageView subviews. When I set the rasterizationScale = 2.0, the images became crisper, but this caused serration to reemerge. To fix this, I created a containerView that held the UIView and UIIMageViews (which were previously subviews of the UIView) and applied rasterizationScale = 1.0 to the UIView and rasterizationScale = 2.0 to the UIImageViews. Now everything looks quite nice.

我有一个类似的问题——一个旋转的UIView和几个UIImageView子视图。当我设置rasterizationScale = 2.0时,图像变得更加清晰,但这导致锯齿重新出现。为了解决这个问题,我创建了一个包含UIView和UIIMageViews(它们以前是UIView的子视图)的containerView,并对UIView应用了rasterizationScale = 1.0,对UIIMageViews应用了rasterizationScale = 2.0。现在一切看起来都很好。

#1


120  

Seems I needed to set rasterizationScale to the proper value for the device as follows.

似乎我需要将rasterizationScale设置为设备的适当值,如下所示。

myView.layer.rasterizationScale = [[UIScreen mainScreen] scale];

#2


0  

I had a similar problem-- a rotated UIView with several UIImageView subviews. When I set the rasterizationScale = 2.0, the images became crisper, but this caused serration to reemerge. To fix this, I created a containerView that held the UIView and UIIMageViews (which were previously subviews of the UIView) and applied rasterizationScale = 1.0 to the UIView and rasterizationScale = 2.0 to the UIImageViews. Now everything looks quite nice.

我有一个类似的问题——一个旋转的UIView和几个UIImageView子视图。当我设置rasterizationScale = 2.0时,图像变得更加清晰,但这导致锯齿重新出现。为了解决这个问题,我创建了一个包含UIView和UIIMageViews(它们以前是UIView的子视图)的containerView,并对UIView应用了rasterizationScale = 1.0,对UIIMageViews应用了rasterizationScale = 2.0。现在一切看起来都很好。