我想用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();但是图像不太适合..。
发布于 2014-03-25 19:11:01
我会这样做:
通过这样的操作,您可以创建一个圆圈形状的剪裁路径:
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 ...设置裁剪路径将导致绘图只发生在路径中。还原状态时,它将删除剪裁路径(或者将其设置为以前的值,通常允许它绘制到整个画布)。
https://stackoverflow.com/questions/22642136
复制相似问题