2

我需要旋转 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;

问题是边界根本不平滑:

模拟器的屏幕截图

4

3 回答 3

3

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

于 2011-11-27T12:19:17.643 回答
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;

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

于 2011-12-31T10:59:29.540 回答
1

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

#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 类型的视图。

于 2014-10-03T18:24:56.393 回答