只绘制 ImageVIew 框架的角

Draw only corners to ImageVIew frame

我试图只绘制 ImageView 框架的角。为此,我使用了 CAShapeLayer 和 Bezier 路径,并在 UIImageView 框架周围使用虚线很好地工作。但要求更像是,必须只显示角落。到目前为止我尝试过的是,

@property (strong, nonatomic)CAShapeLayer *border;

_border = [CAShapeLayer layer];
_border.strokeColor = [UIColor colorWithRed:67/255.0f green:37/255.0f blue:83/255.0f alpha:1].CGColor;
_border.fillColor = nil;
_border.lineDashPattern = @[@40, @50];
[pImageView.layer addSublayer:_border];
_border.path = [UIBezierPath bezierPathWithRect:pImageView.bounds].CGPath;
_border.frame = pImageView.bounds;

我使用了不同的 lineDashPattern 但无法获得结果,因为我没有找到 lineDashPattern 的正确文档。它是如何绘制的等等。 任何类型的帮助都会有用

我想要这样的东西

使用下面的代码,为我工作:

    //1. For imageView Border
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path =[UIBezierPath bezierPathWithRect:_imgView.bounds].CGPath;
    shapeLayer.strokeColor = [UIColor redColor].CGColor; //etc...
    shapeLayer.lineWidth = 2.0; //etc...
    shapeLayer.position = CGPointMake(0, 0); //etc...
    [_imgView.layer addSublayer:shapeLayer];


    //2. For Outside Corner

    float cWidth=30.0;

    float cSpace=_imgView.frame.size.width+20-(2*cWidth);

    NSNumber *cornerWidth=[NSNumber numberWithFloat:cWidth];
    NSNumber *cornerSpace=[NSNumber numberWithFloat:cSpace];

    CAShapeLayer *_border = [CAShapeLayer layer];

    _border.strokeColor = [UIColor blackColor].CGColor;

    _border.fillColor = nil;

    _border.lineWidth=2.0;

    //imageView frame is (width=240,height=240) and 30+200+30=260 -->10 pixel outside imageView
    _border.lineDashPattern = @[cornerWidth,cornerSpace,cornerWidth,@0,cornerWidth,cornerSpace,cornerWidth,@0,cornerWidth,cornerSpace,cornerWidth,@0,cornerWidth,cornerSpace,cornerWidth];

    _border.path = [UIBezierPath bezierPathWithRect:CGRectMake(-10, -10, _imgView.frame.size.width+20, _imgView.frame.size.height+20)].CGPath;

    [_imgView.layer addSublayer:_border];

输出: