如何在IOS中以不同的形状在imageview中设置图像?

时间:2014-01-18 14:10:21

标签: ios imageview shape

我正在IOS中开发一个应用程序,我想在其中以附加图像中给出的形状设置图像。我怎样才能做到这一点?请建议我。

enter image description here

2 个答案:

答案 0 :(得分:1)

一个小时的努力工作。最后......我也添加了绿色边框。

首先,您只需定义可剪裁图像的路径。

- (CGPathRef) pathInRect:(CGRect)rect radius:(CGFloat)radius
{
    CGMutablePathRef retPath = CGPathCreateMutable();
    CGPathMoveToPoint(retPath, NULL, rect.origin.x + radius, rect.origin.y + 2 * radius);

    CGPathAddLineToPoint(retPath, NULL, rect.origin.x, rect.origin.y + 2 * radius);
    CGPathAddLineToPoint(retPath, NULL, rect.origin.x, rect.origin.y + radius);

    CGPathAddArcToPoint(retPath, NULL, rect.origin.x, rect.origin.y, rect.origin.x + radius, rect.origin.y, radius);
    CGPathAddArcToPoint(retPath, NULL, rect.origin.x + 2 * radius, rect.origin.y, rect.origin.x + radius * 2, rect.origin.y + radius, radius);
    CGPathAddArcToPoint(retPath, NULL, rect.origin.x + 2 * radius, rect.origin.y + 2 * radius, rect.origin.x + radius, rect.origin.y + 2 * radius, radius);

    CGPathCloseSubpath(retPath);
    return retPath;
}

在我的旧项目中,我正在使用drawRect for UITableViewCell,所以我重用了代码。代码首先根据路径创建一个新的剪切图像,然后绘制图像。之后,也会绘制具有相同路径的边框。

#define PHOTO_SIDE 40.f
-(void)drawRect:(CGRect)rect {
    CGRect frame = CGRectMake(20, 20, PHOTO_SIDE, PHOTO_SIDE);


    //Draw clipped image
    UIImage *image = [Your Image];
    UIGraphicsBeginImageContext(CGSizeMake(PHOTO_SIDE, PHOTO_SIDE));
    CGPathRef path = [self pathInRect:frame radius:PHOTO_SIDE / 2];
    CGContextAddPath(context, path);
    CGContextClip(context);
    [image drawInRect:CGRectMake(0, 0, PHOTO_SIDE, PHOTO_SIDE)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [newImage drawInRect:frame];

    //Draw Border
    CGContextSaveGState(context);
    [[UIColor greenColor] setStroke];
    CGContextSetLineWidth(context, 1.0f);
    CGContextAddPath(context, path);
    CGContextStrokePath(context);
    CGContextRestoreGState(context);
}

Test Image

答案 1 :(得分:0)

你可以举例如:

  1. 使用蒙版图片覆盖您的imageView(其中包含您希望实际图像可见的透明像素 - 如内有孔的正方形)

  2. maskLayer添加您想要的形状imageView

  3. 创建圆形图像的UIImage类别示例,以便您了解:

    + (instancetype)maskedImageWithImage:(UIImage *)image {
        if (!image)
            return nil;
        CGFloat dim = MIN(image.size.width, image.size.height);
        CGSize size = CGSizeMake(dim, dim);
        UIGraphicsBeginImageContextWithOptions(size, NO, .0);
        UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:(CGRect){ CGPointZero, size }];
        [bezierPath fill];
        [bezierPath addClip];
        CGPoint offset = CGPointMake((dim - image.size.width) * 0.5, (dim - image.size.height) * 0.5);
        [image drawInRect:(CGRect){ offset, image.size }];
        UIImage *ret = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return ret;
    }
    

    使用它像:

    UIImage *myImage = [UIImage imageNamed:@"image"];
    myImageView.image = [UIImage maskedImageWithImage:myImage];
    

    要实现更复杂的形状,您基本上需要修改bezierPath,它实际上是一个剪贴蒙版。