如何创建这种类型的uiimageview?

时间:2015-09-23 07:49:26

标签: ios objective-c uiimageview uiimage

banner of this image

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

3 个答案:

答案 0 :(得分:0)

这样做的最好方法是创建一个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?

请参阅这些示例,然后按照自己的方式调整视图。

答案 1 :(得分:0)

  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);

    }

答案 2 :(得分:0)

目前我在类别中为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];