我不知道为什么它停止工作,但是当我尝试绘制它的任何部分时,这段代码会使我的设备崩溃。我是核心图形的新手,所以任何指示或建议都会有很大帮助。谢谢!
// Style
CGContextRef context = UIGraphicsGetCurrentContext();
// Colors
CGColorRef fillBox = [UIColor colorWithRed:250.0/255.0 green:250.0/255.0 blue:250.0/255.0 alpha:1.0].CGColor;
CGColorRef fillBoxShadow = [UIColor colorWithRed:77.0/255.0 green:77.0/255.0 blue:77.0/255.0 alpha:1.0].CGColor;
CGRect box = CGRectMake(5, 5, self.frame.size.width - 10, self.frame.size.height - 10);
// Shadow
CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 1.0, fillBoxShadow);
CGContextAddRect(context, box);
CGContextFillPath(context);
// Box
CGContextSetFillColorWithColor(context, fillBox);
CGContextAddRect(context, box);
CGContextFillPath(context);
最佳答案
如果您的项目正在使用 ARC,那么这两行可能是您的问题的一部分:
CGColorRef fillBox = [UIColor colorWithRed:250.0/255.0 green:250.0/255.0 blue:250.0/255.0 alpha:1.0].CGColor;
CGColorRef fillBoxShadow = [UIColor colorWithRed:77.0/255.0 green:77.0/255.0 blue:77.0/255.0 alpha:1.0].CGColor;
ARC 正在释放 UIColor 对象,从而释放 CGColorRef。您需要保留 CGColorRef,然后在使用完后释放它。
我会这样写代码:
UIColor *fillBox = [UIColor colorWithRed:250.0/255.0 green:250.0/255.0 blue:250.0/255.0 alpha:1.0];
UIColor *fillBoxShadow = [UIColor colorWithRed:77.0/255.0 green:77.0/255.0 blue:77.0/255.0 alpha:1.0];
然后在方法中稍后使用 fillBox.CGColor 和 fillBoxShadow.CGColor。
关于ios - quartz 二维绘图崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14649690/