我正在尝试保存和恢复CGContext以避免第二次进行繁重的绘图计算而我收到了错误<Error>: CGGStackRestore: gstack underflow.
我究竟做错了什么?这样做的正确方法是什么?
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
if (initialized) {
CGContextRestoreGState(context);
//scale context
return;
}
initialized = YES;
//heavy drawing computation and drawing
CGContextSaveGState(context);
}
Run Code Online (Sandbox Code Playgroud) 我正在使用一个应用程序,我用它CGContextShowTextAtPoint来显示屏幕上的文本.我想显示日文字符,但是CGContextShowTextAtPoint输入一个C字符串.所以要么A)如何将日文字符更改为C字符串?如果无法做到这一点,B)如何在屏幕上手动打印日文字符(在drawRect方法中).
提前致谢.
它是在主视图显示之前调用的
我们可以随时打电话吗?
使用CoreGraphics(在我的drawRect方法中),我正在尝试将混合模式应用于图像(透明png),然后调整结果的alpha.我假设这需要分两步完成,但我可能错了.这是我到目前为止(工作正常):
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
//SET COLOR - EDIT... added a more practical color example
CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 1);
//flips drawing context (apparently this is necessary)
CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);//flip context
//DRAW PIN IMAGE
UIImage *pin = [UIImage imageNamed:@"pin"];
CGRect pinrect = CGRectMake(12, 17, 25, 25);
CGContextDrawImage(context, pinrect, pin.CGImage);//draws image in context
//Apply blend mode
CGContextSetBlendMode(context, kCGBlendModeColor);
CGContextClipToMask(context, pinrect, pin.CGImage); // restricts drawing to within alpha channel
//fills context with mask, applying blend mode
CGContextFillRect(context, pinrect); …Run Code Online (Sandbox Code Playgroud) 我正在尝试为iPad绘制一个简单的绘图应用程序,你可以在图片上绘图,我正在使用CGContext的东西来做,但我最初计划处理擦除的方式是用白色绘制的东西. .除了我刚才意识到当你画到另一张图像时它不起作用,因为当你"擦除"时你也会"擦除"背景图像.
有没有办法支持实际擦除?
谢谢!
我正在使用CGContextStrokePath为图表绘制~768点.问题是每一秒我都会得到一个新的数据点,从而重绘图形.目前这已经是繁忙的应用程序占用了50%的CPU.


图形绘制在UIView中的drawRect中完成.该图是基于时间的,因此新数据点总是到达右侧.
我正在考虑一些替代方法:
也有可能我错过了一些明显的东西,我看到这么糟糕的表现?
CGContextBeginPath(context);
CGContextSetLineWidth(context, 2.0);
UIColor *color = [UIColor whiteColor];
CGContextSetStrokeColorWithColor(context, [color CGColor]);
…
CGContextAddLines(context, points, index);
CGContextMoveToPoint(context, startPoint.x, startPoint.y);
CGContextClosePath(context);
CGContextStrokePath(context);
Run Code Online (Sandbox Code Playgroud) 我面临着一个奇怪的问题MKMapView.我用了一个MKOverlayRenderer.现在的问题是当我缩小图像显示正确时.但是在放大的情况下,图像的某些部分会被切断.它看起来像MapView覆盖层上方的一部分.以下是我的叠加渲染器代码.
class MapOverlayRenderer: MKOverlayRenderer {
var overlayImage: UIImage
var plan: Plan
init(overlay: MKOverlay, overlayImage: UIImage, plan: Plan) {
self.overlayImage = overlayImage
self.plan = plan
super.init(overlay: overlay)
}
override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in ctx: CGContext) {
let theMapRect = overlay.boundingMapRect
let theRect = rect(for: theMapRect)
// Rotate around top left corner
ctx.rotate(by: CGFloat(degreesToRadians(plan.bearing)));
// Draw the image
UIGraphicsPushContext(ctx)
overlayImage.draw(in: theRect, blendMode: CGBlendMode.normal, alpha: 1.0)
UIGraphicsPopContext();
}
func degreesToRadians(_ x:Double) -> Double { …Run Code Online (Sandbox Code Playgroud) 我想用线性CGGradient填充我通过Core Graphics绘制的多边形形状.CGContextDrawLinearGradient函数从一个点到另一个点绘制渐变,但它填充整个视图.如何仅在我绘制的多边形内部显示渐变?
我在绘制一些用颜色描边的线条然后用另一条线填充它们的内部(它们形成一个多边形)时遇到了麻烦.
UIColor *houseBorderColor = [UIColor colorWithRed:(170/255.0) green:(138/255.0) blue:(99/255.0) alpha:1];
CGContextSetStrokeColorWithColor(context, houseBorderColor.CGColor);
CGContextSetLineWidth(context, 3);
// Draw the polygon
CGContextMoveToPoint(context, 20, viewHeight-19.5);
CGContextAddLineToPoint(context, 200, viewHeight-19.5); // base
CGContextAddLineToPoint(context, 300, viewHeight-119.5); // right border
CGContextAddLineToPoint(context, 120, viewHeight-119.5);
CGContextAddLineToPoint(context, 20, viewHeight-19.5);
// Fill it
CGContextSetRGBFillColor(context, (248/255.0), (222/255.0), (173/255.0), 1);
//CGContextFillPath(context);
// Stroke it
CGContextStrokePath(context);
Run Code Online (Sandbox Code Playgroud)
随着CGContextStrokePath注释,我得到这个结果:

但是,如果我取消注释CGContextStrokePath并填充多边形,颜色会溢出笔划:

你如何实现这样的结果(无需重做整个绘图程序两次):

我需要创建一个包含一行文本的图像.但问题是,我首先需要创建上下文(CGBitmapContextCreate),要求图像的宽度和高度有可能以后计算文本的边界(通过CTLineGetImageBounds).但我想要图像的大小=文本的边界:(我该怎么办?
实际上我用
CGBitmapContextCreate
CTLineGetImageBounds
CTLineDraw
Run Code Online (Sandbox Code Playgroud)
也许可以在没有上下文的情况下调用CTLineGetImageBounds?
注意:我在delphi上,这不是一个真正的问题,因为我可以访问所有的API,我只需要函数名称