OGeek|极客世界-中国程序员成长平台

标题: ios - 如何使 UIImageView 中心的区域透明 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 08:38
标题: ios - 如何使 UIImageView 中心的区域透明

我有一个 UIImageView,我想让 ImageView 中心的一个圆形区域透明,这样当图像传递到 ImageView 时,图像似乎覆盖了除了 ImageView 之外的所有 ImageView 中心。

这是我目前拥有的 我想看的是背景,而不是图像中的那个黑色圆圈。

有一个类似的问题here但我需要有人教我如何在代码中做到这一点,因为我并不真正了解 Core Graphics。

任何帮助将不胜感激。



Best Answer-推荐答案


这样就可以了:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50.0, 50.0, 100.0, 100.0)];
[imageView setBackgroundColor:[UIColor redColor]];

UIBezierPath *maskPath = [UIBezierPath bezierPathWithOvalInRect:imageView.bounds];

UIBezierPath *holePath = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(imageView.bounds, 35.0, 35.0)];
[maskPath appendPath:holePath];

CAShapeLayer *mask = [CAShapeLayer layer];
[mask setFillRule:kCAFillRuleEvenOdd];
mask.path = maskPath.CGPath;

[imageView.layer setMask:mask];

[self.view addSubview:imageView];

你给'holePath'的插图越多,内圆的半径就越小;)

关于ios - 如何使 UIImageView 中心的区域透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24744801/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4