如何在ios中对imageview进行六边形图像遮罩?

时间:2014-06-02 05:54:07

标签: ios iphone objective-c xcode xcode5

我想在iOS中的图像视图上实现图像蒙版。 图像蒙版应为带圆角的六边形。 一些第三方代码可用,但他们没有圆角。

请帮帮我。因为我是iOS新手。

1 个答案:

答案 0 :(得分:5)

根据您显示图像的方式,您可以通过几种不同的方式对其进行掩饰。

案例1:CALayer

您可以使用CALayer上的mask属性。此属性为CALayer*类型,并将目标图层适当地屏蔽到给定图层的Alpha通道。请注意,此属性可以是CALayer的子类,特别是CAShapeLayer,它允许您指定掩码的直接路径(此处可以指定六边形)。另请注意,确保在蒙版图层上设置一些不透明的颜色,以使蒙版图层实际上被遮罩。

注意:如果您需要帮助绘制六边形的路径让我知道,我最近做了今天早些时候的几何:D

案例2:CoreGraphics

您可以像这样使用CGImageMaskCreateCGImageCreateWithMask

- (UIImage *)maskImage:(UIImage *)image withMask:(UIImage *)mask {

  CGImageRef cgmask = CGImageMaskCreate(CGImageGetWidth(mask.CGImage), CGImageGetHeight(mask.CGImage), CGImageGetBitsPerComponent(mask.CGImage), CGImageGetBitsPerPixel(mask.CGImage), CGImageGetBytesPerRow(mask.CGImage), CGImageGetDataProvider(mask.CGImage), NULL, false);
  CGImageRef cgmaskedImage = CGImageCreateWithMask(image.CGImage, cgmask);
  UIImage *retval = [UIImage imageWithCGImage:maskedImageRef];
  CGImageRelease(cgmask);
  CGImageRelease(cgmaskedImage);

  return retval;
}

掩码图像应该是您想要遮罩的黑色。这样你就可以在photoshop中创建一个六边形,然后使用这个方法创建一个蒙版图像。如果需要的话,它会自动缩放蒙版图像。

我个人更喜欢使用CALayer,因为即使UIImageView也有CALayer支持,我对CALayer非常熟悉。这就是说我不确定哪个是更高效或正确的方法。

编辑:添加六边形图代码。

这里我将创建一些代码来创建一个代表六边形的CGPathRef。

  UIView *v = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
  v.backgroundColor = [UIColor purpleColor];

  CGRect rect = v.frame;

  CAShapeLayer *hexagonMask = [CAShapeLayer layer];
  UIBezierPath *hexagonPath = [UIBezierPath bezierPath];
  CGFloat sideWidth = 2 * ( 0.5 * rect.size.width / 2 );
  CGFloat lcolumn = ( rect.size.width - sideWidth ) / 2;
  CGFloat rcolumn = rect.size.width - lcolumn;
  CGFloat height = 0.866025 * rect.size.height;
  CGFloat y = (rect.size.height - height) / 2;
  CGFloat by = rect.size.height - y;
  CGFloat midy = rect.size.height / 2;
  CGFloat rightmost = rect.size.width;
  [hexagonPath moveToPoint:CGPointMake(lcolumn, y)];
  [hexagonPath addLineToPoint:CGPointMake(rcolumn, y)];
  [hexagonPath addLineToPoint:CGPointMake(rightmost, midy)];
  [hexagonPath addLineToPoint:CGPointMake(rcolumn, by)];
  [hexagonPath addLineToPoint:CGPointMake(lcolumn, by)];
  [hexagonPath addLineToPoint:CGPointMake(0, midy)];
  [hexagonPath addLineToPoint:CGPointMake(lcolumn, y)];

  hexagonMask.path = hexagonPath.CGPath;
  v.layer.mask = hexagonMask;

您可以将图片视图替换为v,它应自动屏蔽它。

编辑:带边框的六边形的代码清单。请注意,此示例中的lineWidth仅显示为2.5,因为其中一半将被剪裁。如果您想要一个真实的5像素边框,请将lineWidth指定为10

  UIView *v = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
  v.backgroundColor = [UIColor purpleColor];

  CGRect rect = v.frame;

  CAShapeLayer *hexagonMask = [CAShapeLayer layer];
  CAShapeLayer *hexagonBorder = [CAShapeLayer layer];
  hexagonBorder.frame = v.layer.bounds;
  UIBezierPath *hexagonPath = [UIBezierPath bezierPath];
  CGFloat sideWidth = 2 * ( 0.5 * rect.size.width / 2 );
  CGFloat lcolumn = ( rect.size.width - sideWidth ) / 2;
  CGFloat rcolumn = rect.size.width - lcolumn;
  CGFloat height = 0.866025 * rect.size.height;
  CGFloat y = (rect.size.height - height) / 2;
  CGFloat by = rect.size.height - y;
  CGFloat midy = rect.size.height / 2;
  CGFloat rightmost = rect.size.width;
  [hexagonPath moveToPoint:CGPointMake(lcolumn, y)];
  [hexagonPath addLineToPoint:CGPointMake(rcolumn, y)];
  [hexagonPath addLineToPoint:CGPointMake(rightmost, midy)];
  [hexagonPath addLineToPoint:CGPointMake(rcolumn, by)];
  [hexagonPath addLineToPoint:CGPointMake(lcolumn, by)];
  [hexagonPath addLineToPoint:CGPointMake(0, midy)];
  [hexagonPath addLineToPoint:CGPointMake(lcolumn, y)];

  hexagonMask.path = hexagonPath.CGPath;
  hexagonBorder.path = hexagonPath.CGPath;
  hexagonBorder.fillColor = [UIColor clearColor].CGColor;
  hexagonBorder.strokeColor = [UIColor blackColor].CGColor;
  hexagonBorder.lineWidth = 5;
  v.layer.mask = hexagonMask;
  [v.layer addSublayer:hexagonBorder];