gpt4 book ai didi

iphone - 使用 Core Graphics 绘制部分图像或对角线裁剪

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:16:22 26 4
gpt4 key购买 nike

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

真实图像:enter image description here

所需的图像输出:enter image description here

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

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

现在,我想在此图形层上绘制图像而不是在其中填充颜色。我没有办法沿对角线裁剪此图像,因为我们只能传递原点 x、y 和尺寸高度、宽度等参数。

最佳答案

您应该能够将上下文剪切到上下文的当前路径。剪切上下文后所做的任何操作都将仅在该区域可见:

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

这个有用吗?

关于iphone - 使用 Core Graphics 绘制部分图像或对角线裁剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9850772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com