【问题标题】:UIImage fill transparent part with another imageUIImage 用另一个图像填充透明部分
【发布时间】:2014-03-25 17:32:08
【问题描述】:

我想在 iOS 中用另一张图片填充图片的透明部分。

我用UIGraphicsContext 尝试了一些东西,但我无法让它工作,因为我以前从未使用过它。

图片如下:

我试过的一些代码:

UIImageView *v = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 70, 70)];

UIGraphicsBeginImageContextWithOptions(v.frame.size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
// beach image
UIImage *img = [UIImage imageNamed:@"zivogosce_camping_05.jpg"];
//[img drawInRect:CGRectMake(2, 0, 12, 12)];

UIImage *annotationImg = [UIImage imageNamed:@"custom_annotation"];
[annotationImg drawAtPoint:CGPointMake(0, 0)];

CGContextSetFillColorWithColor(context, [[UIColor colorWithPatternImage:img] CGColor]);
CGContextFillRect(context, CGRectMake(0, 0, 50, 50));


CGContextDrawImage(context, CGRectMake(0, 0, v.frame.size.width, v.frame.size.height), [annotationImg CGImage]);



UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

[v setImage:newImage];

UIGraphicsEndImageContext();

但是图片不太合适...

【问题讨论】:

  • 你为什么不把它画在另一张图片上?
  • 请发布一些代码,以便我们帮助您确定问题所在。你现在画得怎么样?

标签: ios uiimage transparent fill uigraphicscontext


【解决方案1】:

我会这样做:

您可以通过执行以下操作来制作圆形的剪切路径:

CGContextSaveGState(context);
CGRect circleRect = CGRectMake(circleCenter.x - radius, circleCenter.y - radius, radius * 2.0, radius * 2.0);
CGContextAddEllipseInRect (context, circleRect);
CGContextClip(context);

// ... do whatever you need to in order to draw the inner image ...

CGContextRestoreGState(context);

// ... draw the transparent png ...

设置剪切路径将导致仅在路径内进行绘制。当您恢复状态时,它会删除剪切路径(或者更确切地说,将其设置回之前的值,这通常允许它绘制到整个画布)。

【讨论】:

  • 请举个例子
猜你喜欢
  • 1970-01-01
  • 2014-05-11
  • 2020-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多