- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当我调用函数 CGContextStrokePath(下面代码的最后一行)时,我的程序崩溃了。上下文是否会以某种方式损坏? (它只有在 expression
(它是一个 NSArray)中有特定值时才会崩溃)。它应该绘制 expression
中的任何内容的图表。例如,如果 expression
具有以下对象:x, cos(表示为字符串),它将绘制余弦曲线。这是代码:
- (double) yValueFromExpression:(id)anExpression atPosition:(double)xValue
{
NSDictionary *aDictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithDouble:xValue] forKey:@"%x"];
return [CalculatorBrain evaluateExpression:anExpression usingVariableValues:aDictionary];
}
#define PRECISION 500
- (void)drawRect:(CGRect)rect
{
double scale = [self.delegate scaleForGraphView:self];
id expression = [self.delegate expressionForGraphView:self];
CGPoint origin;
origin.x = (self.bounds.origin.x + self.bounds.size.width) / 2;
origin.y = (self.bounds.origin.y + self.bounds.size.height) / 2;
[AxesDrawer drawAxesInRect:self.bounds originAtPoint:origin scale:scale];
// -150/scale to 150/scale is the range of x values that axesDrawer (drawAxesInRect) displays.
double leftMostXValue = -150 / scale;
double rightMostXValue = 150 / scale;
double increment = (rightMostXValue - leftMostXValue) / PRECISION;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextMoveToPoint(context, self.bounds.origin.x, origin.y -
[self yValueFromExpression:expression atPosition:leftMostXValue] * scale);
for (int i = 1; i <= PRECISION; ++i) {
double currentXValue = leftMostXValue + i * increment;
CGContextAddLineToPoint(context, self.bounds.origin.x + (self.bounds.size.width / PRECISION) * i,
origin.y - [self yValueFromExpression:expression atPosition:currentXValue] * scale);
}
CGContextStrokePath(context);
}
这是我在调用 CGContextStrokePath 时收到的错误消息:程序收到信号:“EXC_BAD_ACCESS”。
答案:我需要在 CGContextAddLineToPoint() 周围放置一个保护装置,以确保它在 rect
的范围内绘制:
for (int i = 1; i <= PRECISION; ++i) {
double currentXValue = leftMostXValue + i * increment;
double xPoint = self.bounds.origin.x + (self.bounds.size.width / PRECISION) * i;
double yPoint = origin.y - [self yValueFromExpression:expression atPosition:currentXValue] * scale;
if (xPoint < (self.bounds.origin.x + self.bounds.size.width) && xPoint > 0 &&
yPoint < (self.bounds.origin.y + self.bounds.size.height) && yPoint > 0) {
CGContextAddLineToPoint(context, xPoint, yPoint);
}
}
最佳答案
EXC_BAD_ACCESS 通常意味着你有内存问题。
如果非要我猜的话,expressionForGraphView 有时可能会返回垃圾。
关于iphone - CGContextRef 损坏了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5239165/
我假设原点在左下角,y轴从0开始向上扩展。 x 轴从 0 开始向右扩展。 我只是问,因为我从绘图代码中得到了非常奇怪的结果......想确保我没有弄错这部分。 最佳答案 这取决于此上下文的使用位置。
如何将使用 CGBitmapContextCreate() 创建的 CGContextRef 绘制到 NSView? 我应该先将其转换为图像吗?如果是这样的话,这不是一个昂贵的操作吗? 最佳答案 Sh
我尝试在 ipad 上创建一个简单的手指绘画应用程序。我能够正确地绘制到屏幕的路径,但我希望有一个选项可以完全清除所有绘制路径的屏幕。我目前拥有的代码清除了上下文,但是当我调用绘图代码块时,所有路径都
我正在制作一个类似于绘图应用程序的应用程序,并希望在用户触摸的地方绘制图像。我可以在 O.K. 的位置绘制图像。使用此代码: CGContextRef ctx = UIGraphicsGetCurre
我想画一个圆弧并填充它。第一张图是这样的。我想得到第二张图的效果 - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFra
所以,我有这段代码,它画了一个圆圈,周围有一条线。 - (void)drawRect:(CGRect)rect { CGContextRef contextRef = UIGraphicsGe
我有一个 CGContextRef,里面画了一些东西,我想把它清除掉,让它是空的(全透明的)。怎么做? 最佳答案 CGContextClearRect应该完成这项工作。 关于ios - 如何在绘图之前
当我调用函数 CGContextStrokePath(下面代码的最后一行)时,我的程序崩溃了。上下文是否会以某种方式损坏? (它只有在 expression(它是一个 NSArray)中有特定值时才会
由于对我之前的结果不满意,我被要求创建一个徒手绘图 View ,该 View 在缩放时不会模糊。我能想到这是可能的唯一方法是使用 CATiledLayer,否则在缩放时画线会太慢。目前,我已将其设置为
如何将 CGContextAddArc 中的所有像素点存储到 NSMutableArray. 或 CGContextRef 到 NSMutable Array static inline float
在我的应用程序中,我需要调整和裁剪一些本地和在线存储的图像。我正在使用Trevor Harmon's tutorial它实现了UIImage+Resize。 在我的 iPhone 4(iOS 4.3.
我刚刚开始学习 OSX 编程,我偶然发现了一个问题,我不知道是否应该使用 CGContextRef 还是 NSGraphicsContext。 有什么区别? 我几乎不知道 CG 或 NS 之间的区别[
在重写 UIView drawRect 时,我使用 CGContextDrawImage 绘制主图像。在它上面我需要绘制另一个图像(使用多重混合模式),所以我实际上需要在它上面绘制。 第二个图像需要准
我想在 UIImageView 上绘制一个“消失的笔画”,它会在触摸事件之后在固定时间延迟后自动删除。这是我的 ViewController 中的内容。 - (void)touchesMoved:(N
这个问题已经存在: 关闭 10 年前。
我有很多时间都在忍受一个问题,就是简单地绘制两个单独的形状(例如矩形) 具体来说,我有一个数据源,它是一个数组,这个数组包含包含 CGPoint 的子数组。我想做的只是根据每个子数组绘制路径,并且它们
我有我的 UIView 的框架,在 drawRect 中它使用它作为上下文,但我希望它在这个上下文之外绘制。我如何指定我自己的上下文,让它这样做? 最佳答案 我不明白您如何将框架用作上下文。框架是 C
如何画一条有形的线?例如带有 CGContextRef 的心形、菱形或星形线? 心线示例: 菱形线示例: 三角线示例: 星线示例: 这是我目前拥有的: - (void)draw { CGCon
这个问题在这里已经有了答案: Assigning an existing CGColor to a CGColor property works in iOS Simulator, not iOS
目前,我在绘制小点时遇到图形问题。 我注意到在大多数专业日历应用程序中,事件日历标识符是一个小点,其颜色是事件日历颜色。 我目前正处于申请阶段,我需要画一个更好的点。这是我的意思的照片。 它在这里可能
我是一名优秀的程序员,十分优秀!