我想使用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。
你应该能够将上下文剪辑到上下文的当前路径上。任何你做的事情后,剪下的上下文将是可见的,只有在该区域。
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];
这样可以吗?