2012-09-25 139 views
3

我一直在嘗試在圓形路徑中剪裁圖像。但是,我似乎無法使裁剪路徑成爲反鋸齒。修剪圖像的抗鋸齒

我已經嘗試了幾個不同的東西,但似乎沒有工作。我如何獲得修剪後的圖像以消除鋸齒?

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

+0

我也一直在尋找解決方案... –

+0

我得到了一個解決方案:http://stackoverflow.com/q/14071278/384864 –

回答

1

我有這個問題,我花了幾個小時,對谷歌和調試,但我終於想通這一個。

發生什麼事情是,當您將maskedImage設置爲imageView.image時,會發生一些調整,其中包括圓形裁剪(在圖像上),結果出現時髦。

一個小例子是,如果你的imageView有一個100×100邊界和maskedImage具有300×300的bounds,那麼當imageView.image設置maskedImage會有降低調整以適應圖像。這種縮小尺寸不會更新圓形裁剪,所以你的結果看起來是鋸齒狀的邊緣。如果在添加圓形裁剪之前,您將maskedImage縮放至接近imageView的大小,則圓角邊緣將完全按照您的需要!