Ios 剪切图像的抗混叠

Ios 剪切图像的抗混叠,ios,uiimage,antialiasing,uibezierpath,Ios,Uiimage,Antialiasing,Uibezierpath,我一直在尝试在圆形路径中剪切图像。但是,我似乎无法对剪切路径进行抗锯齿处理 我试过几种不同的方法,但似乎都不管用。如何对剪裁后的图像进行抗锯齿处理 UIImage*originalImage = [UIImage imageNamed:@"grandpa.png"]; int minWidth = originalImage.size.width; bool isWidth = YES; if(minWidth > originalImage.size.height){ m

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

我试过几种不同的方法,但似乎都不管用。如何对剪裁后的图像进行抗锯齿处理

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

我遇到了这个问题,在谷歌上花了我几个小时进行调试,但我最终解决了这个问题

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


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

我也一直在寻找解决方案……我得到了一个解决方案: