如何创建这种类型的 uiimageview?

How to create this type of uiimageview?

我正在创建这样的个人资料视图,但如何创建这样的背景横幅图片?此横幅图片将是动态的。

最好的方法是创建一个 UIImageView 的子类,甚至是一个包含图像的 UIView(为此记得剪裁子视图)并覆盖此方法:

- (void)drawRect:(CGRect)rect

你可以在这里找到一个例子,他制作了一个圆形的 UIView。 : How to draw a custom UIView that is just a circle - iPhone app

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextAddEllipseInRect(ctx, rect);
    CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor blueColor] CGColor]));
    CGContextFillPath(ctx);
}

根据这些信息,您可以继续制作其他形状,如下所示: Drawing a polygon with one color for stroke, and a different one for fill?

所以获取这些示例,并按照您想要的方式塑造您的观点。

  you can iverride drwarect method in image view code for hardcoded value for iphone 4 
 - (void)drawRect:(CGRect)rect
    {
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGMutablePathRef a_path = CGPathCreateMutable();
            CGContextBeginPath(context);
        CGContextSetLineWidth(context, 3);

        // Draw the polygon
        CGContextMoveToPoint(context, 0, 0);
        CGContextAddLineToPoint(context, 320, 0); // base
        CGContextAddLineToPoint(context, 320, 80); // right border
        CGContextAddLineToPoint(context, 0, 120);
        CGContextAddLineToPoint(context, 0, 0);


        // Stroke it
        CGContextClosePath(context);
        CGContextAddPath(context, a_path);
        // Fill it
        CGContextSetRGBFillColor(context, (0/255.0), (0/255.0), (0/255.0), 0);
        //CGContextFillPath(context);

            CGContextFillPath(context);
            CGPathRelease(a_path);

    }

目前我为类别中的 UIImageView 执行此操作。

CAShapeLayer *mask=[CAShapeLayer new];
mask.frame=self.layer.bounds;
CGMutablePathRef path = CGPathCreateMutable();
CGFloat width = self.layer.frame.size.width;
CGFloat height = self.layer.frame.size.height;

CGPathMoveToPoint(path, nil, 0, 0);
CGPathAddLineToPoint(path, nil, width, 0);
CGPathAddLineToPoint(path, nil, width, height);
CGPathAddLineToPoint(path, nil, 0, height/2);
CGPathAddLineToPoint(path, nil, 0, 0);
CGPathCloseSubpath(path);

mask.path = path;
CGPathRelease(path);

self.layer.mask = mask;

CAShapeLayer *shape = [CAShapeLayer layer];
shape.frame = self.bounds;
shape.path = path;
shape.lineWidth = 3.0f;
shape.strokeColor = [UIColor whiteColor].CGColor;
[self.layer insertSublayer: shape atIndex:0];