【问题标题】:Line is not smooth on layer.border when its rotated旋转时,layer.border 上的线条不平滑
【发布时间】:2012-01-07 07:01:53
【问题描述】:

我需要旋转一个 UIImageView:

UIImageView *img = [[UIImageView alloc] init];
img.layer.borderWidth = 3.0;
img.layer.borderColor = UIColorFromRGB(0xffffff).CGColor;
img.layer.shadowColor = UIColorFromRGB(0x000000).CGColor;
CATransform3D transform = CATransform3DMakeRotation ([Helper degreesToRadians:(5)], 1, 1, 1);
img.layer.shadowRadius  = 2.0;
img.layer.shouldRasterize = YES;

问题是边框一点都不光滑:

【问题讨论】:

    标签: ios transform calayer catransform3d


    【解决方案1】:

    尝试在图像周围添加透明边框(可能为 1px)。请参阅此link。 检查该页面中的UIImage+Alpha 部分。

    【讨论】:

    • 这是唯一的选择吗?有没有办法在没有这条蹩脚线的情况下旋转?
    【解决方案2】:

    我看到这个问题的大部分答案是添加 1px 透明边框,但它只有在我们不使用 UIImageView 周围的任何边框时才有效,如果 UIImageView 本身有边框并且我们旋转它,那么边框不平滑。我仍在试图弄清楚如何解决这个问题。

    这是我为解决此问题所做的工作

     UIImage *thumbnail = [photo thumbnailImage:thumbnailSize.width 
                               transparentBorder:2 
                                    cornerRadius:0 
                            interpolationQuality:kCGInterpolationHigh];
    
      CGPoint randomPoint = [self getRandomOriginPoint];
    
      UIImageView *thumbnailView = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 10.0, thumbnail.size.width, thumbnail.size.height)];
    
      [thumbnailView.layer setBorderWidth:2];
      [self setContentMode:UIViewContentModeCenter];
      [thumbnailView.layer setBorderColor:[UIColor whiteColor].CGColor];
      [thumbnailView.layer setShadowOffset:CGSizeMake(-3.0, 3.0)];
      [thumbnailView.layer setShadowRadius:3.0];
      [thumbnailView.layer setShadowOpacity:1.0];
      thumbnailView.layer.shouldRasterize = YES;
    

    我使用来自here 的 UIImage+Resize.h 在我的代码中获取缩略图,因此即使您以任意角度旋转图像后,边框看起来也非常完美!

    【讨论】:

    • 我用这个答案来解决我的问题,然后用提供更少假设的解决方案重新回答。真正解决问题的是光栅化。
    【解决方案3】:

    无需使用类别,做出最少假设的解决方案

    #import <QuartzCore/QuartzCore.h>
    

    我们将使用以下变量

    UIView *myView;
    

    这已被确认适用于所有类型的 UIImageViews / UIViews

    myView.layer.shouldRasterize = YES; //the single line that solved this problem from the above
    

    您不需要添加任何类型的边框即可。

     CATransform3D rotate = CATransform3DIdentity;
     myView.layer.shouldRasterize = YES;
     rotate = CATransform3DRotate(rotate,M_PI_4/8,0.0,0.0,1);
     myView.layer.transform = rotate;
    

    注意:提供此答案是为了将信息整合到一个答案中,从而消除了一些其他要求。在此示例中,我使用 M_PI_4/8,因为如果您正在实现一个 Stickie Note 类型的视图,它是一个不错的视角。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      相关资源
      最近更新 更多