首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIImage用另一个图像填充透明部分

UIImage用另一个图像填充透明部分
EN

Stack Overflow用户
提问于 2014-03-25 17:32:08
回答 1查看 819关注 0票数 1

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

我在UIGraphicsContext上尝试过一些东西,但是我无法让它发挥作用,因为我以前从未使用过它。

这是一张图片:

我试过的一些代码:

代码语言:javascript
复制
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();

但是图像不太适合..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-25 19:11:01

我会这样做:

通过这样的操作,您可以创建一个圆圈形状的剪裁路径:

代码语言:javascript
复制
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 ...

设置裁剪路径将导致绘图只发生在路径中。还原状态时,它将删除剪裁路径(或者将其设置为以前的值,通常允许它绘制到整个画布)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22642136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档