gpt4 book ai didi

ios - 创建渐变并返回方法

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

对于有关 iPhone 和 Quartz 编程的菜鸟问题,我们深表歉意。刚刚开始我从 C++ 到 Objective-C 的转换:)

所以,我有这样一个类方法

+(CGGradientRef)CreateGradient:(UIColor*)startColor endColor:(UIColor*)endColor
{
CGGradientRef result;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[2] = {0.0f, 1.0f};
CGFloat startRed, startGreen, startBlue, startAlpha;
CGFloat endRed, endGreen, endBlue, endAlpha;

[endColor getRed:&endRed green:&endGreen blue:&endBlue alpha:&endAlpha];
[startColor getRed:&startRed green:&startGreen blue:&startBlue alpha:&startAlpha];

CGFloat componnents[8] = {
startRed, startGreen, startBlue, startAlpha,
endRed, endGreen, endBlue, endAlpha
};
result = CGGradientCreateWithColorComponents(colorSpace, componnents, locations, 2);
CGColorSpaceRelease(colorSpace);
return result;
}

及其用法。

-(void)FillGradientRect:(CGRect)area startColor:(UIColor *)startColor endColor:(UIColor *)endColor isVertical:(BOOL)isVertical
{
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
CGGradientRef gradient = [Graphics CreateGradient:startColor endColor:endColor];

CGPoint startPoint, endPoint;
if (isVertical) {
startPoint = CGPointMake(CGRectGetMinX(area), area.origin.y);
endPoint = CGPointMake(startPoint.x, area.origin.y + area.size.height);
}else{
startPoint = CGPointMake(0, area.size.height / 2.0f);
endPoint = CGPointMake(area.size.width, startPoint.y);
}

CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);

CGGradientRelease(gradient);
UIGraphicsPopContext();
}

一切正常。但是,当我从 Xcode 4 运行分析工具时,我在方法 CreateGradient 中收到有关 result 变量内存泄漏的警告。好吧,我明白那是怎么回事,但在我的调用方法中我释放了渐变对象 (CGGradientRelease(gradient);)。那么,谁错了,如何让分析工具开心呢?

谢谢

最佳答案

由于 CGGradientRef 是 Core Foundation 类型的对象,您可以自动释放它。只需在返回渐变之前添加这一行:

[(id)result autorelease];

关于ios - 创建渐变并返回方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8032797/

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