- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我收到大量 CGContextDrawPath:无效上下文 0x0。如果您想查看回溯,请设置 CG_CONTEXT_SHOW_BACKTRACE 环境变量。
此代码中的错误。它在 SKScene 中执行。基本上,我将用户的绘图作为 SKShapeNode 显示在屏幕上。然后我通过 Core Graphics 用圆圈填充该绘图的路径。但是代码运行缓慢并且给我带来了很多错误。我对 Swift 有点陌生。你能帮我看看这是怎么回事吗?我怎样才能加快速度?如何为 CoreGraphics 提供适当的上下文?
let boundingBox = createdShape.frame
for j in stride(from: Int(boundingBox.minX), to: Int(boundingBox.maxX), by: 10) {
for i in stride(from: Int(boundingBox.minY), to: Int(boundingBox.maxY), by: 10) {
//for i in 0..<10 {
counter += 1
//let originalPoint = CGPoint(x: ((boundingBox.maxX - boundingBox.minX)/2)+boundingBox.minX, y: (boundingBox.maxY-CGFloat(i*10)))
//let originalPoint = CGPoint(x: CGFloat(j), y: (boundingBox.maxY-CGFloat(i*10)))
let originalPoint = CGPoint(x: CGFloat(j), y: (CGFloat(i)))
let point:CGPoint = self.view!.convert(originalPoint, from: self)
if (createdShape.path?.contains(createdShape.convert(originalPoint, from: self)))! {
let circlePath = UIBezierPath(arcCenter: point, radius: CGFloat(5), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)
circlePath.fill()
let shapeLayer = CAShapeLayer()
shapeLayer.path = circlePath.cgPath
shapeLayer.fillColor = UIColor(red: 180/255, green: 180/255, blue: 180/255, alpha: 0.4).cgColor
shapeLayer.strokeColor = UIColor(red: 180/255, green: 180/255, blue: 180/255, alpha: 0.4).cgColor
shapeLayer.lineWidth = 0.0
view!.layer.addSublayer(shapeLayer)
}
}
}
最佳答案
看看你的代码,我相信代码的罪魁祸首是:
circlePath.fill()
由于 UIBezierPath.fill()
是 Core Graphics 绘制操作,因此需要在 Core Graphics 上下文中调用它,以便它知道实际绘制的位置。
这通常在 UIGraphicsBeginImageContextWithOptions()
内部完成/UIGraphicsEndImageContext()
在您显式创建和结束上下文的地方,或者在某些 UIKit 方法(例如 UIView.drawRect()
)中,上下文会自动为您管理。
从外观上看,在代码的那部分调用 fill()
是在存在的上下文之外完成的,这就是它报告无效上下文 0x0 的原因。
在这种特殊情况下,看起来您正在使用 circlePath
对象作为 CAShapeLayer
的剪切蒙版,因此可能没有必要调用 fill()
那里。
如果您需要进一步说明,请告诉我。 :)
关于ios - CoreGraphics 绘制结果到 CGContextDrawPath : invalid context 0x0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40473226/
Xcode 10 更改了搜索头文件的方法。具体来说,它给出了警告: Traditional headermap style is no longer supported; please migrate
我的基于 Objective-C 的项目中有一些 swift 代码文件。 我可以编译它,它们相互配合得很好。 swift 代码可以 导入 CoreGraphics.CGBase 然而,当我在调试swi
我的形状周围有这条线: 问题是,它的厚度显然从 1px 变成了 2px。这是我正在使用的代码: CGContextRef context = UIGraphicsGetCurrentContext()
我有以下代码,似乎唯一使用整行的最后一种颜色......我希望颜色在整个过程中不断变化。有什么想法吗? CGContextSetLineWidth(ctx, 1.0);
我试图在 CG 中获得透明背景,但它总是显示为黑色。 我有以下代码: - (id)initWithFrame:(CGRect)frame { if ((self = [super initWi
我使用以下代码对加载的图像执行一些操作,但我发现在视网膜显示屏上时显示变得模糊 - (UIImage*)createImageSection:(UIImage*)image section:(CGRe
我正在尝试绘制具有2px透明边框的图像: CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetRGBFillColor(
我已经使用CoreGraphics绘制了两个四边形(4面)形状的路径。总共有6个点,path1使用前4个点,path2使用后4个点,因此都共享2个点。 代码如下 - (void)drawRect:(C
我正在编写一个左侧有图例的 ViewController(标签和彩色框的垂直列表:Category1:黄色,Category2:gree,Category 3:蓝色,等等......) 用户可以点击列
我的形状周围有这条线: 问题是,它的厚度显然从 1px 变成了 2px。这是我正在使用的代码: CGContextRef context = UIGraphicsGetCurrentContext()
我有一个名为 myView 的 UIView @interface MyView : UIView { UIImage *myPic; NSMutableArray *myDra
我需要帮助来绘制这样的东西: 有人告诉我,灰色背景条和紫色条应该绘制在不同的图层上。然后,表示一本书章节(此 slider 所涉及的内容)的点将位于这两层之上的一层上。 我已经完成了在事件栏上创建渐变
有人可以帮我用 CoreGraphics 绘制一个特定的渐变吗?只要给出线索就足够了。示例代码值得我致以最深的敬意。我需要这个形状: 我尝试了以下代码: -(UIImage *)imageWithGr
我的 CGContext 有一个路径和一个线宽,这在我的 UIView 中绘制了一个漂亮的黑色路径轮廓。 .伟大的。现在我试着给这个笔划上色,虽然 CGContextSetStrokeColorWit
我有一个UIBezierPath,它是一个弧线。我只想在图形上下文中绘制笔划(即圆弧的笔划,不包括连接圆弧终点和起点的线) 完成此操作后,我可以向上下文添加线性渐变,从而有效地在圆弧笔划上绘制渐变。
我正在尝试使用核心图形创建两个矩形路径。当我尝试使用颜色填充路径时,重叠的路径不会填充颜色。 使用的代码是 - (void)drawRect:(CGRect)rect { CGPoint to
编辑:我认为最好的是将代码转换为 CoreAnimation。该代码将如何转换为该方法?我认为问题在于设备 FPS 存在差异。 我一直在为 iOS 搜索自定义进度圈并遇到这个库:https://git
我肯定遗漏了 Core Graphics 上下文的某些部分,无法自行清除。这是我在 Xcode Playground 中的代码 import UIKit class MyView08 : UIView
我不确定是否可以提高图像质量,但以下代码在 PDF 中显示的图像质量非常差。我知道它用于从 View 生成图像的标准代码,但我可以做些什么来指定图像质量或改进它吗? - (void)renderVie
此代码来自 Apple 的 WWDC 2011 session 318 - 深入了解 iOS 性能并使用 CoreGraphics 从服务器托管图像创建缩略图。 CGImageSourceRef sr
我是一名优秀的程序员,十分优秀!