【发布时间】:2020-05-17 17:55:25
【问题描述】:
我希望使用 Core Graphics 绘制图像。但是我能够绘制部分图像,即水平和垂直。但不是它,我想以对角线方式拥有它。如需更多说明,请参阅下图:
实图:
所需的图像输出:
下面是使用颜色填充图层的代码:
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(contextRef, r.origin.x, r.origin.y );
CGContextAddLineToPoint(contextRef, r.origin.x , r.origin.y);
CGContextAddLineToPoint(contextRef, r.origin.x + (r.size.width ), r.origin.y + (r.size.height));
CGContextAddLineToPoint(contextRef, r.origin.x , r.origin.y + (r.size.height));
CGContextAddLineToPoint(contextRef, r.origin.x , r.origin.y);
CGContextSetRGBFillColor(contextRef, [[color objectAtIndex:0]floatValue]/255.0f, [[color objectAtIndex:1]floatValue]/255.0f, [[color objectAtIndex:2]floatValue]/255.0f, .5);
CGContextSetRGBStrokeColor(contextRef,[[color objectAtIndex:0]floatValue]/255.0f, [[color objectAtIndex:1]floatValue]/255.0f, [[color objectAtIndex:2]floatValue]/255.0f, 0.5);
CGContextFillPath(contextRef);
现在,我想在此图形层上绘制图像,而不是在其中填充颜色。我没有任何方法可以对角裁剪这张图片,因为我们只能传递像 Origin x,y & Size Height, Width 这样的参数。
【问题讨论】:
-
您可以使用CGPath在路径中创建对角线和剪辑图像。
-
@Vignesh 我将使用 CGContextRef 和图像来绘制形状。但问题是如何对角切割该图像?
-
你必须剪辑上下文。 stackoverflow.com/a/3668156/641062
-
在google上使用蒙版图片搜索,你会发现一些开源实现。这可能会完成你的工作mattgemmell.com/2010/07/05/mgimageutilities
-
对不起,上面的链接对你有用。 stackoverflow.com/questions/2776747/masking-a-uiimage
标签: iphone ios uiimage core-graphics ios-simulator