【问题标题】:Change the default shape of UIImageview shape rectangle更改 UIImageview 形状矩形的默认形状
【发布时间】:2014-01-13 15:21:24
【问题描述】:

现在我正在开发一个项目,我需要显示带有如下形状边框的图像。

我该怎么做?我不知道这样做。请任何想法解决。

【问题讨论】:

  • 是静态图片还是控件,需要在用户触摸图片时处理触摸事件?您是在图像编辑器中创建图像还是需要以编程方式创建图像?
  • @yurish:static image..从互联网下载的用户图像..然后在上面的步骤中显示图像
  • 我接受了下面的答案,但它不会给出我上面提到的确切形状...
  • @user2922837 好的,没有问题兄弟...
  • 要构建您需要的形状,请使用[UIBezierPath bezierPath] 创建 maskPath,然后使用 UIBezierPath 类的addLineToPoint:... addArcWithCenter: ... closePath: 方法添加每个形状段。跨度>

标签: ios objective-c uiimageview


【解决方案1】:

也许这段代码会对你有所帮助...

 UIBezierPath *maskPath;
 maskPath = [UIBezierPath bezierPathWithRoundedRect:YourImageVIew.bounds byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight) cornerRadii:CGSizeMake(50.0, 50.0)];

 CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
 maskLayer.frame = YourImageVIew.bounds;
 maskLayer.path = maskPath.CGPath;
 YourImageVIew.layer.mask = maskLayer;

【讨论】:

  • :你能介绍一下imgtest吗?
  • 抱歉,这是你的原图
  • @user2922837 现在检查我的更新答案...我在我的应用程序中检查此代码,它对我有用。
  • 从你的方法来看,我没有得到我想要的确切形状
  • @user2922837 根据您的要求更改cornerRadii值
【解决方案2】:

以下是创建掩蔽路径的方法:

- (UIBezierPath *)curvedRectWithFrame:(CGRect)frame radius:(CGFloat)radius
{
    double halfFrameHeight = ((double)frame.size.height / 2);

    // Check if the radius is too small.
    if (radius < halfFrameHeight) {
        radius = halfFrameHeight;
    }

    CGFloat arcAngle = asin(halfFrameHeight/radius);
    CGFloat centerX = frame.origin.x + (frame.size.width - radius);
    CGFloat centerY = frame.origin.y + halfFrameHeight;

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:frame.origin];
    [path addLineToPoint:CGPointMake(centerX + radius * cos(arcAngle), frame.origin.y)];
    [path addArcWithCenter:CGPointMake(centerX, centerY) radius:radius startAngle:-arcAngle endAngle:arcAngle clockwise:YES];
    [path addLineToPoint:CGPointMake(frame.origin.x, path.currentPoint.y)];
    [path closePath];

    return path;
}

然后您可以将形状蒙版应用到您的图像:

const CGFloat kCurveRadius = 500.;

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = yourImageView.bounds;
maskLayer.path = [self curvedRectWithFrame:maskLayer.bounds radius:kCurveRadius].CGPath;

yourImageView.layer.mask = maskLayer;

【讨论】:

    猜你喜欢
    • 2011-02-19
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 2016-04-19
    • 1970-01-01
    相关资源
    最近更新 更多