使用Core Graphics绘制部分图像或对角剪裁 [英] Draw partial image or clipping diagonally using Core Graphics

查看:157
本文介绍了使用Core Graphics绘制部分图像或对角剪裁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用Core Graphics绘制图像。但是我能够绘制部分图像,即水平和水平。垂直。但不是它,我希望以对角线方式使用它。有关更多说明,请参考下图:

I am looking to draw an image using Core Graphics.However I am able to draw the partial image i.e Horizontally & vertically. But instead of it I want to have it in a Diagonal Way.For more clarification, pls refer to below image :

真实图像: < img src =https://i.stack.imgur.com/fiB5e.pngalt =在此处输入图像说明>

Real Image :

必需的图像输出:

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

    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& amp;尺寸高度,宽度。

Now, I want to Draw an image on this Graphics Layer rather than filling color in it.I didn't got any way to crop this image Diagonally since we can only pass parameters like Origin x,y & Size Height, Width.

提前致谢。

推荐答案

你应该能够将上下文剪辑到上下文的当前路径。剪切上下文后所做的任何操作只能在该区域中显示:

You should be able to clip the context to the context's current path. Anything you do after having clipped the context will be visible only in that area:

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

这有用吗?

这篇关于使用Core Graphics绘制部分图像或对角剪裁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆