4

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

我尝试了一些不同的东西,但似乎都没有。如何使剪切的图像具有抗锯齿功能?

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();

锯齿图像

4

1 回答 1

1

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

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

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

于 2013-12-03T12:56:57.167 回答