Tag: cgcontext

目标c renderInContext在后台线程上崩溃

我有一个应用程序,其中屏幕不断在后台线程捕获。 这是代码 – (UIImage *) captureScreen { UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; CGRect rect = [keyWindow bounds]; UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); [[keyWindow layer] renderInContext:context]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIDeviceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight) || (orientation==UIInterfaceOrientationPortraitUpsideDown)) { img=[self rotatedImage:img]; } return img; } 它适用于捕捉一次或两次。 但一段时间后,应用程序总是崩溃在同一行[[keyWindow […]

创buildUIImageView时无效的上下文

我尝试创buildUIImageView时出现错误。 看看这个代码: UIImage* backgroundPanel = [[UIImage imageNamed:@"loginPanelBackground.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(90, 0, 149, 416)]; self.connexionBackgroundImgView = [[UIImageView alloc] initWithImage:backgroundPanel]; self.connexionBackgroundImgView.frame = CGRectMake(0, 0, 416, 390); // THIS LINE PROVOC THE INVALID CONTEXT [self.connexionView insertSubview:self.connexionBackgroundImgView aboveSubview:self.connexionToCreationCompteView]; 它会在日志中引发这个错误: <Error>: CGContextSaveGState: invalid context 0x0 <Error>: CGContextSetBlendMode: invalid context 0x0 <Error>: CGContextSetAlpha: invalid context 0x0 <Error>: CGContextTranslateCTM: invalid context 0x0 <Error>: CGContextScaleCTM: […]

CALayer与CGContext,这是一个更好的devise方法?

我一直在做一些iOS绘图的实验。 为了做一个实际的练习,我写了一个BarChart组件。 下面是类图(呃,我不允许上传图片),所以让我用文字来写。 我有一个NGBarChartViewinheritance自UIView有2个协议NGBarChartViewDataSource和NGBarChartViewDelegate。 代码是在https://github.com/mraghuram/NELGPieChart/blob/master/NELGPieChart/NGBarChartView.m 为了绘制barChart,我创build了每个barChart作为不同的CAShapeLayer。 我这样做的原因有两个,首先我可以创build一个UIBezierPath并将其附加到一个CAShapeLayer对象和两个,我可以通过使用[Layer hitTest]方法轻松跟踪是否触摸barItem。 该组件工作得很好。 然而,我不习惯用我绘制barCharts的方法。 因此,这个笔记。 我需要关于以下方面的专家意见 通过使用CAShapeLayer和创buildBarItems我真的不使用UIGraphicsContext,这是一个很好的devise? 我的方法将在UIView中创build几个CALayers。 基于性能的限制,你可以在UIView中创buildCALayers的数量。 如果一个好的select是使用CGContext *方法,那么怎样才能确定一个特定的path是否被触及 从animation的angular度来看,比如当你点击它时Bar闪烁,是Layerdevise更好还是CGContextdevise更好。 非常感谢帮助。 顺便说一句,你可以自由地看我的代码和评论。 我会很乐意接受任何改善build议。 最好的,Murali

使用CGContextAddArcToPoint()绘制圆弧时,(x1,y1)和(x2,y2)是什么意思?

你可以使用下面的代码来使用Quartz绘制一个弧: CGContextMoveToPoint(context2, x, y); CGContextAddArcToPoint(context2, x1, y1, x2, y2, r); 在这些函数中, (x,y)是起点, r是圆弧半径,但是(x1,y1)和(x2,y2)什么?

iOS:提高图像绘制的速度

我有一个图像序列,我想animation( UIImageView支持一些基本的animation,但它不足以满足我的需要)。 我的第一个方法是使用UIImageView并设置image属性时,图像。 这太慢了。 速度不佳的原因是由于图像的绘制(这令我感到惊讶,我认为瓶颈将会加载图像)。 我的第二种方法是使用通用的UIView并设置view.layer.contents = image.CGImage 。 这并没有明显的改善。 这两种方法都必须在主线程上执行。 我认为速度不佳是由于不得不将图像数据绘制到CGContext 。 我怎样才能提高绘图速度? 是否有可能在后台线程上的上下文?

使用CGContext绘图后擦除

我试图做一个简单的绘图应用程序的iPad,你可以画一张图片,我使用CGContext的东西来做,但我原本计划处理擦除的方式是只是画白色的东西..除了我今天才意识到,当你正在绘制另一个图像时,它不工作,因为那么当你“擦除”时,你也将“擦除”背景图像。 有什么办法来支持实际的擦除? 谢谢!

CGContext:如何擦除位图上下文之外的像素(例如kCGBlendModeClear)?

我正在尝试使用Core Graphics构build一个橡皮擦工具,而且我发现制作高性能橡皮的难度令人难以置信 – 这一切都归结为: CGContextSetBlendMode(context, kCGBlendModeClear) 如果你谷歌周围如何与核心graphics“擦除”,几乎每个答案都回来这个片段。 问题是它只是(显然)在位图上下文中工作。 如果你正在尝试实现交互式擦除,我不明白kCGBlendModeClear如何帮助你的 – 就我所知,你或多或less被locking在擦除UIImage / CGImage的屏幕上和在着名的非高性能[UIView drawRect] 。 这是我所能做到的最好的: -(void)drawRect:(CGRect)rect { if (drawingStroke) { if (eraseModeOn) { UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0); CGContextRef context = UIGraphicsGetCurrentContext(); [eraseImage drawAtPoint:CGPointZero]; CGContextAddPath(context, currentPath); CGContextSetLineCap(context, kCGLineCapRound); CGContextSetLineWidth(context, lineWidth); CGContextSetBlendMode(context, kCGBlendModeClear); CGContextSetLineWidth(context, ERASE_WIDTH); CGContextStrokePath(context); curImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); [curImage drawAtPoint:CGPointZero]; } else { [curImage drawAtPoint:CGPointZero]; […]

我怎么能比CGContextStrokePath更快地渲染线?

我正在绘制使用CGContextStrokePath的graphics〜768点。 问题是每一秒我都会得到一个新的数据点,从而重新绘制graphics。 目前,这已经是一个繁忙的应用程序占用50%的CPU。 graphics绘制是在UIView的drawRect中完成的。 该图是基于时间的,所以新的数据点总是到达右侧。 我正在考虑一些替代方法: 用GLKit绘图(以不支持旧设备为代价),看起来像很多工作。 做一些屏幕抓取(renderInContext?),向左移1px,blit,只为最后两个数据点绘制一条线。 有一个非常广泛的CALayer和泛沿? 平滑的数据集,但这感觉就像作弊:) 这也有可能是我错过了一些明显的东西,我看到这样糟糕的performance? 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);

擦除UIImageView

如何擦除前UIImage上的内容,通过滑动手指并显示UIImage的背面UIImage。 我使用了下面的代码- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event擦除前面的图像: -(void)eraseDrawingContents { UIGraphicsBeginImageContext(frontImage.frame.size); [frontImage.image drawInRect:CGRectMake(0, 0, frontImage.frame.size.width, frontImage.frame.size.height)]; CGContextSaveGState(UIGraphicsGetCurrentContext()); CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES); CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 25.0); CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(), CGSizeMake(0, 0), 50, [[UIColor whiteColor]CGColor]); CGMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path, nil, lastPoint.x, lastPoint.y); CGPathAddLineToPoint(path, nil, currentPoint.x, currentPoint.y); CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear); CGContextAddPath(UIGraphicsGetCurrentContext(), path); CGContextStrokePath(UIGraphicsGetCurrentContext()); frontImage.image = UIGraphicsGetImageFromCurrentImageContext(); CGContextRestoreGState(UIGraphicsGetCurrentContext()); UIGraphicsEndImageContext(); lastPoint = currentPoint; } 我实现了以下输出。 但是我需要这样的输出。 […]

在iOS中擦除绘图

我正在制作一个绘图应用程序,我有一个UIBezeirPath,用它在touchesMoved中绘制,并将其转换为CGPath,然后绘制到CGlayer, 这是我的代码 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { self.currentPath = [[DrawingPath alloc] init]; if(m_eraseButtonClicked) { [self.currentPath setPathColor:[UIColor backgroundColor]]; } else { [self.currentPath setPathColor:self.lineColor]; } CGPathRef cgPath = self.currentPath.path.CGPath; mutablePath = CGPathCreateMutableCopy(cgPath); } – (void)Erase { CGContextRef layerContext = CGLayerGetContext(self.DrawingLayer); CGContextSetBlendMode(layerContext,kCGBlendModeClear); CGContextSetLineWidth(layerContext, self.eraseWidth); CGContextBeginPath(layerContext); CGContextAddPath(layerContext, mutablePath); CGContextStrokePath(layerContext); } – (void)UndoRedoFunction { //Undo/Redo for(int i =0; i<[undoArray count];i++) […]