gpt4 book ai didi

ios - CoreGraphics 矩形渐变

转载 作者:行者123 更新时间:2023-11-29 03:37:44 24 4
gpt4 key购买 nike

有人可以帮我用 CoreGraphics 绘制一个特定的渐变吗?只要给出线索就足够了。示例代码值得我致以最深的敬意。我需要这个形状:

enter image description here

我尝试了以下代码:

-(UIImage *)imageWithGradientBorder
{
CGRect rect=CGRectMake(0, 0, self.size.width, self.size.height);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef ctx= CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, 8, rect.size.width, colorSpace, kCGImageAlphaNone);
CGGradientRef glossGradient;
size_t num_locations = 4;
CGFloat locations[4] = { 0.0, 0.1, 0.9, 1.0 };
CGFloat components[16] = { 0, 0, 0, 1, 1.0, 1.0, 1.0, 1, 1.0, 1.0, 1.0, 1, 0, 0, 0, 1};
glossGradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, num_locations);
CGRect currentBounds = rect;

CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f);
CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMaxY(currentBounds));
topCenter = CGPointMake( 0.0f, CGRectGetMidY(currentBounds));
midCenter = CGPointMake(CGRectGetMaxX(currentBounds), CGRectGetMidY(currentBounds));

CGContextDrawLinearGradient(ctx, glossGradient, topCenter, midCenter, 0);
CGImageRef mask = CGBitmapContextCreateImage(ctx);
CGContextRelease(ctx);
CGImageRef maskedImageRef = CGImageCreateWithMask([self CGImage], mask);
CGImageRelease(mask);
UIImage *maskedImage = [UIImage imageWithCGImage:maskedImageRef];
CGImageRelease(maskedImageRef);
return maskedImage;
}

但它只从左侧绘制渐变。

最佳答案

topCenter = CGPointMake( 0.0f, CGRectGetMidY(currentBounds));
midCenter = CGPointMake(CGRectGetMaxX(currentBounds), CGRectGetMidY(currentBounds));

这两个变量的名称具有误导性,它们并不是真正的中上和中中,而是中左和中右。所以当你打电话时:

CGContextDrawLinearGradient(ctx, glossGradient, topCenter, midCenter, 0);

您正在从左到右绘制渐变。

您需要做什么来组织您的点并绘制渐变 4 次(L->R、R->L、T->B、B->T)。根据您希望角落看起来准确的样子,您可能需要在每个渐变绘制之间进行一些 mask ,以防止渐变一直绘制到角落中。

关于ios - CoreGraphics 矩形渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18918382/

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