图片旋转时的边缘抗锯齿的三种方法以及性能

首先,系统给出的layer中的抗锯齿方法,简单粗暴,一个开关就搞定了。
layer.allowsEdgeAntialiasing = true

第二种,在info.plist中加入键值:
"Renders with edge antialiasing" YES 即可。

第三种,给图像加上1px的透明,UIImage的category方法如下所示:

- (UIImage *)antiAlias
{
CGFloat border = 1.0f;
CGRect rect = CGRectMake(border, border, self.size.width-2*border, self.size.height-2*border);
UIImage *img = nil;

UIGraphicsBeginImageContext(CGSizeMake(rect.size.width,rect.size.height));
[self drawInRect:CGRectMake(-1, -1, self.size.width, self.size.height)];
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIGraphicsBeginImageContext(self.size);
[img drawInRect:rect];
UIImage* antiImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return antiImage;
}

前两种是系统给出的方法,简单粗暴,但是第三种比较另类但是如果屏幕上出现大批量图片的时候,该方法可以提供更好的性能。

在第三种方法中,不能只用UIGraphicsBeginImageContext(imageRect.size);否则图像会变模糊。 应该再使用UIGraphicsBeginImageContextWithOptions

评论

此博客中的热门博文

使__attribute__((annotate("xxx")))能作用于OC函数

viewWillAppear不执行的解决办法

iOS中使用xpc/xpc.h