iPhone以编程方式裁剪方形图像以显示为圆形 [英] iPhone programmatically crop a square image to appear as circle
问题描述
我正在尝试使用iPhone上相机胶卷中的图像为自定义样式UIButton创建图像。该按钮具有圆形背景,实际上显示为圆形。现在我需要一个图像进入按钮的中间,该图像也显示为圆形。
I'm trying to create an image for a custom style UIButton using an image from the camera roll on iPhone. The button has a circular background and effectively appears as a circle. Now I need an image to go in the middle of the button that also appears round.
如何将方形UII图像切割成圆形区域以外的透明度?
How do I cut a square UIImage to appear round with transparency outside of the round area?
如果屏蔽涉及到,我需要预先渲染一个面具,还是可以通过编程方式创建一个(例如:圆圈)?
If masking is involved, do I need to pre-render a mask or can I create one programmatically(ex: a circle)?
谢谢!
推荐答案
是的,您可以使用 CoreGraphics
动态绘制遮罩。
然后你可以创建蒙面图像。
Yes you can use CoreGraphics
to draw the mask dynamically.
Then you can create the masked image.
蒙版示例:
- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage
{
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef maskedImageRef = CGImageCreateWithMask([image CGImage], mask);
UIImage *maskedImage = [UIImage imageWithCGImage:maskedImageRef];
CGImageRelease(maskedImageRef);
CGImageRelease(mask);
return maskedImage;
}
这篇关于iPhone以编程方式裁剪方形图像以显示为圆形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!