ios 图片 视图 锯齿问题整理

来源:互联网 发布:程序员书单 编辑:程序博客网 时间:2024/05/09 20:09
XCode的info.plist里面有以下两项可以开启抗锯齿
Renders with edge antialisasing = YES (UIViewEdgeAntialiasing)
Renders with group opacity = YES (UIViewGroupOpacity)
但是我发现,当我在视图里放图片,然后视图加阴影后,移动产生了锯齿。

视图内抗锯齿处理
在UIView的drawRect方法里为当前视图打开抗锯齿:
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetAllowsAntialiasing(context, true);
    CGContextSetShouldAntialias(context, true);
}

还有一种说法,给图片增加一个像素的透明边框,说可以解决锯齿问题,但是我试了不成功。。。
CGFloat border = 1;
CGRect imageRect = CGRectMake(0, 0, img.size.width, img.size.height);
UIGraphicsBeginImageContext(imageRect.size);
[img drawInRect:CGRectMake(border,border,img.size.width-border*2,img.size.height-border*2)];
UIImage* newImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

另外,View.layer.shouldRasterize = YES;也是很重要的一点。