剪裁图像的抗锯齿

时间:2012-09-25 14:52:41

标签: ios uiimage antialiasing uibezierpath

我一直在尝试在圆形路径中剪切图像。但是,我似乎无法使剪切路径消除锯齿。

我尝试了一些不同的东西,但似乎都没有。如何使剪裁图像消除锯齿?

UIImage*originalImage = [UIImage imageNamed:@"grandpa.png"];
int minWidth = originalImage.size.width;
bool isWidth = YES;
if(minWidth > originalImage.size.height){
        minWidth = originalImage.size.height;
        isWidth = NO;
}

UIBezierPath *path;
if(isWidth)
    path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, (originalImage.size.height - minWidth)/2, minWidth, minWidth)];
else
    path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake((originalImage.size.width - minWidth)/2, 0, minWidth, minWidth)];


UIGraphicsBeginImageContextWithOptions(originalImage.size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetAllowsAntialiasing(context, true);
CGContextSetShouldAntialias(context, true);
[path addClip];
[originalImage drawAtPoint:CGPointZero];
UIImage *maskedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//This is based on the full size image
CGRect imageRect = CGRectMake(0, 0, maskedImage.size.width + 2, maskedImage.size.height + 2);
UIGraphicsBeginImageContext(imageRect.size);
[maskedImage drawInRect:CGRectMake(1,1,maskedImage.size.width,maskedImage.size.height)];
maskedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Aliasing Image

1 个答案:

答案 0 :(得分:1)

我遇到了这个问题,我花了几个小时在谷歌和调试,但我终于想出了这个问题。

当你将maskedImage设置为imageView.image时会发生一些调整大小,其中包括圆形剪裁(在图像上),结果很时髦。

一个小例子是,如果imageView的边界为100x100且maskedImage的边界为300x300,那么当imageView.image设置maskedImage时,maskedImage缩小尺寸以适应图像。这种减小的尺寸不会更新圆形剪裁,因此您的结果似乎是锯齿状边缘。如果在添加圆角剪裁之前将imageView缩放到接近{{1}}的尺寸,那么圆角边缘将完全符合您的要求!