在UIView上绘制图像,并制作透明孔以查看

时间:2015-07-15 17:29:35

标签: ios objective-c uiview uiimage

我有一个UIView我想把它放在一起,它将在另一个视图之上分层。这个新视图需要有一个完全透明的洞。我附上了我想要完成的截图(棋盘格模式是底层的UIView,它将添加这个新的UIView作为子图层,红色是UIImage)。

Black transparent is UIView, red is UIImage

我有以下代码将渲染带有中心孔的黑色背景:

- (void)
drawRect:(CGRect)rect
{
  CGRect boxRect = CGRectMake(_location.x - (kPointRadius/2), _location.y - (kPointRadius/2), kPointRadius, kPointRadius);

  UIBezierPath *path = [UIBezierPath
                        bezierPathWithRoundedRect:CGRectMake(0, 0, self.layer.frame.size.width, self.layer.frame.size.height)
                        cornerRadius:0];

  UIBezierPath *circlePath = [UIBezierPath
                              bezierPathWithRoundedRect:boxRect
                              cornerRadius:kPointRadius];
  [path appendPath:circlePath];
  [path setUsesEvenOddFillRule:YES];

  CAShapeLayer *fillLayer = [CAShapeLayer layer];
  fillLayer.path = path.CGPath;
  fillLayer.fillRule = kCAFillRuleEvenOdd;
  fillLayer.fillColor = [UIColor blackColor].CGColor;
  fillLayer.borderWidth = 5.0;
  fillLayer.borderColor = [UIColor redColor].CGColor;
  fillLayer.opacity = 0.7;
  [self.layer addSublayer:fillLayer];
}

但是,当我向其添加图像并使用[[UIImage imageNamed:@"testimg" drawAtPoint:CGPointMake(x, y)];添加图像时,图像会覆盖该孔。

有人能指出我在正确的方向吗?我很难过。

编辑:我现在几乎可以在那里得到它。我可以得到我需要的一切除了在黑色顶部分层的图像,90%不透明背景也是90%不透明度。

CGRect boxRect = CGRectMake(
                              _location.x - (kPointRadius/2),
                              _location.y - (kPointRadius/2),
                              kPointRadius,
                              kPointRadius);

  UIBezierPath *path = [UIBezierPath
                        bezierPathWithRoundedRect:CGRectMake(0, 0, self.layer.frame.size.width, self.layer.frame.size.height) cornerRadius:0];

  UIBezierPath *circlePath = [UIBezierPath
                              bezierPathWithRoundedRect:boxRect
                              cornerRadius:kPointRadius];
  [path appendPath:circlePath];
  [path setUsesEvenOddFillRule:YES];

  UIImage *image = [UIImage imageNamed:@"testimg"];
  UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(13, 57, 350, 230)];
  imageView.image = image;
  CGRect r = CGRectMake(self.layer.frame.origin.x, self.layer.frame.origin.y, self.layer.frame.size.width, self.layer.frame.size.height);
  UIGraphicsBeginImageContextWithOptions(r.size, NO, 0);
  CGContextRef c = UIGraphicsGetCurrentContext();
  CGContextAddPath(c, CGPathCreateCopy(path.CGPath));

  CGContextEOClip(c);
  CGContextFillRect(c, r);
  UIImage* maskim = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  CALayer* mask = [CALayer layer];
  mask.frame = r;
  mask.contents = (id)maskim.CGImage;
  imageView.layer.mask = mask;
  self.layer.mask = mask;
  self.layer.backgroundColor = [UIColor blackColor].CGColor;
  self.layer.opacity = 0.8;
  [self.layer addSublayer:imageView.layer];

1 个答案:

答案 0 :(得分:1)

  编辑:我现在能够在那里得到它。我可以得到我需要的一切除了在黑色顶部分层的图像,90%不透明背景也是90%不透明度。

您可以将图层的背景颜色设置为[UIColor colorWithWhite:0.0 alpha:0.9],而不是设置其不透明度。