ios - 如何在 UIImageview 中绘制椭圆形

标签 ios iphone objective-c

我想绘制一个椭圆形的UIImageView。我试着画画,但我找不到答案。我展示了大部分圆形或圆形。
enter link description here
我试试这个,但这是行不通的

#import <QuartzCore/QuartzCore.h>`
 CALayer *imageLayer = YourImageview.layer;
            [imageLayer setCornerRadius:5];
            [imageLayer setBorderWidth:1];
            [imageLayer setMasksToBounds:YES];

enter image description here

最佳答案

如果要绘制椭圆形的 ImageView ,请按照以下步骤操作:

  1. 使用 bezierPathWithOvalInRect 创建 UIBezierPath

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:YOUR_RECT];
    
  2. 使用CAShapeLayer 创建 mask 层

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    
  3. 现在将贝塞尔曲线路径设置为 mask 层的路径

    maskLayer.path = path.CGPath;
    
  4. 然后我们将用我们自己的 mask 层 mask 我们的 View 。

    YourImageview.layer.mask = maskLayer;
    

就是这样。试试吧。

关于ios - 如何在 UIImageview 中绘制椭圆形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23779116/

相关文章:

iphone - 如何从 facebook-ios-sdk-3.5.3 获取好友列表

iPhone - 找不到时调试崩溃

iphone - 如何禁用横向?

ios - JSON POST Web 服务中的多个 Json 对象和 3 个字符串

ios - 无法转换 FBSDKGraphRequestHandler 类型的值

ios - PFTableViewCell 不改变 labelText

iphone - 在物理设备上测试

ios - 为什么我在不同的设备上得到不同的 CGFloat 值

ios - 我如何像在 Objective C 宏中声明那样全局声明变量或函数

ios - SLComposeViewController: "No Twitter Accounts"警报的不同行为取决于是否安装 Twitter 应用程序