iphone - 使用 Core Graphics 绘制部分图像或对角线剪裁
<p><p>我希望使用 Core Graphics 绘制图像。但是我能够绘制部分图像,即水平和垂直。但不是它,我想以对角线方式拥有它。如需更多说明,请参阅下图:</p>
<p>真实图片:<img src="/image/fiB5e.png" alt="enter image description here"/> </p>
<p>所需的图像输出:<img src="/image/P3yBz.png" alt="enter image description here"/> </p>
<p>下面是使用颜色填充图层的代码:</p>
<pre><code> 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, [floatValue]/255.0f, [floatValue]/255.0f, [floatValue]/255.0f, .5);
CGContextSetRGBStrokeColor(contextRef,[floatValue]/255.0f, [floatValue]/255.0f, [floatValue]/255.0f, 0.5);
CGContextFillPath(contextRef);
</code></pre>
<p>现在,我想在此图形层上绘制图像,而不是在其中填充颜色。我没有任何方法可以对角裁剪此图像,因为我们只能传递像 Origin x,y & Size Height, Width 这样的参数。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您应该能够将上下文剪辑到上下文的当前路径。您在剪辑上下文后所做的任何事情都将仅在该区域中可见:</p>
<pre><code>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
;
</code></pre>
<p>这行得通吗?</p></p>
<p style="font-size: 20px;">关于iphone - 使用 Core Graphics 绘制部分图像或对角线剪裁,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/9850772/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/9850772/
</a>
</p>
页:
[1]