我一直在尝试在圆形路径中剪切图像。但是,我似乎无法使剪切路径消除锯齿。
我尝试了一些不同的东西,但似乎都没有。如何使剪裁图像消除锯齿?
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();
答案 0 :(得分:1)
我遇到了这个问题,我花了几个小时在谷歌和调试,但我终于想出了这个问题。
当你将maskedImage
设置为imageView.image
时会发生一些调整大小,其中包括圆形剪裁(在图像上),结果很时髦。
一个小例子是,如果imageView
的边界为100x100且maskedImage
的边界为300x300,那么当imageView.image
设置maskedImage
时,maskedImage
缩小尺寸以适应图像。这种减小的尺寸不会更新圆形剪裁,因此您的结果似乎是锯齿状边缘。如果在添加圆角剪裁之前将imageView
缩放到接近{{1}}的尺寸,那么圆角边缘将完全符合您的要求!