使用Core Graphics绘制部分图像或对angular线剪切

我正在寻找使用Core Graphics绘制图像。然而,我能够绘制部分图像,即水平和垂直。 但是,而不是我想要在对angular线方式。更多的澄清,请参考下面的图像:

真实图像: 在这里输入图像说明

所需的图像输出: 在这里输入图像说明

以下是使用颜色填充图层的代码:

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); 

现在,我想在这个graphics层上绘制一个图像,而不是填充颜色。我没有办法裁剪这个图像,因为我们只能传递像Origin x,y&Size Height,Width这样的参数。

提前致谢。

您应该能够将上下文剪辑到上下文的当前path。 剪切了上下文之后所做的任何操作都只能在该区域中看到:

 UIImage *img = ...; 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); // clip context to current path CGContextClip(contextRef); // draw image [img drawInRect:rect]; 

这工作?