UIImage with transparent rounded corners

后端 未结 5 2098
无人共我
无人共我 2020-12-03 15:50

I\'m using following code to add rounded corners to my UIImage, but the problem is that the rounded corners are showing \"white\" area instead of transparent or \"clear\". W

5条回答
  •  庸人自扰
    2020-12-03 16:46

    Here's a simpler formulation using UIKit calls:

    - (UIImage*) roundCorneredImage: (UIImage*) orig radius:(CGFloat) r {
        UIGraphicsBeginImageContextWithOptions(orig.size, NO, 0);
        [[UIBezierPath bezierPathWithRoundedRect:(CGRect){CGPointZero, orig.size} 
                                    cornerRadius:r] addClip];
        [orig drawInRect:(CGRect){CGPointZero, orig.size}];
        UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return result;
    }
    

    Notice the NO parameter - this makes the image context transparent, so the clipped-out region is transparent.

提交回复
热议问题