2012-12-28 88 views
1

我只是測試下面的代碼輪UIImage的角落:UIImage的光滑的圓角石英

- (UIImage *)makeRoundedImage:(UIImage *)image radius:(float)radius; 
{ 
    CALayer *imageLayer = [CALayer layer]; 
    imageLayer.frame = CGRectMake(0, 0, image.size.width, image.size.height); 
    imageLayer.contents = (id) image.CGImage; 

    imageLayer.masksToBounds = YES; 
    imageLayer.cornerRadius = radius; 

    UIGraphicsBeginImageContext(image.size); 
    [imageLayer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return roundedImage; 
} 

,除非你仔細觀察,以生成的圖像本人看起來不錯。角落不光滑。我怎樣才能讓角落更平滑/柔和?

編輯:

圓角處理後的圖像在石英:

enter image description here

就像你所看到的角落裏並不順利。

這是UIImageView的cornerRadius更平滑的版本。

enter image description here

在哪裏抓?

+0

你可以發佈你想什麼樣的形象作物和有什麼結果呢? – Ismael

+0

我不想將圓角應用於UIImageView因爲我想將此圖像保存到文件系統。 –

+0

我剛剛更新了問題... –

回答

4

這裏是UIImage的類別:

- (UIImage *) imageWithRoundedCornersRadius: (float) radius 
{ 
    // Begin a new image that will be the new image with the rounded corners 
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0); 

    // Add a clip before drawing anything, in the shape of an rounded rect 
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height); 
    [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius] addClip]; 

    // Draw your image 
    [self drawInRect:rect]; 

    // Get the image, here setting the UIImageView image 
    UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext(); 

    // Lets forget about that we were drawing 
    UIGraphicsEndImageContext(); 

    return roundedImage; 
} 
+0

我也試過這個,但它是一樣的。問題已通過示例更新。請看看... –

+3

你應該更新你的答案:UIGraphicsBeginImageContextWithOptions(self.size,NO,0.0); –