Tag: cglayer

为CGLayer创buildCGContext

我想在CGLayer预先渲染一些graphics,以便在将来进行快速绘制。 我发现CGLayerCreateWithContext需要一个CGContext参数。 它可以很容易地在drawRect:方法中find。 但是我需要在drawRect:之外创build一个CGLayer 我应该在哪里得到CGContext ? 我应该简单地创build临时CGBitmapContext并使用它? 更新:我需要在drawRect之外创buildCGLayer:因为我想在呈现之前初始化CGLayer。 第一次drawRect调用可以初始化一次,但对我来说这不是一个漂亮的解决scheme。

内存问题与CGlayer

我正在绘制CGlayers。 我已经实现了绘图部分,用户绘制的drawingView(canvas)是dynamic的,我的意思是用户可以增加/减lessdrawingView(Canvas)的高度, 例如默认大小 – 500 * 200 当用户点击扩展button – 500 * 300 所以这里是我的function,当用户扩大canvas, – (void)IncreaseCanavasSize { CGContextRef layerContext1 = CGLayerGetContext(permanentDrawingLayer ); CGContextDrawLayerInRect(layerContext1, rectSize, newDrawingLayer); rectSize = self.bounds; CGFloat scale = self.contentScaleFactor; CGRect bounds = CGRectMake(0, 0, self.bounds.size.width * scale, self.bounds.size.height * scale); CGLayerRef layer = CGLayerCreateWithContext(layerContext1, bounds.size, NULL); CGContextRef layerContext = CGLayerGetContext(layer); CGContextScaleCTM(layerContext, scale, scale); […]

画一个CGlayer到另一个CGlayer

我使用两个CGlayers进行绘制,Layer1和Layer2,我绘制到Layer1和触摸结束,我将数据转移到Layer2,并清除Layer1,并在我的drawRect,方法,我画两个层的上下文。 点击删除button,我将绘图从Layer2转移到Layer1,然后我删除layer1,但触摸结束,我不转移到Layer2,因为我只擦除Layer1 在笔上点击如果擦除发生,我将绘图从Layer1转移到Layer2,否则以前的绘图可能会消失。 在代码中,它的样子 – (void)eraserButtonClicked { CGContextRef layerContext1 = CGLayerGetContext( self.layer1); CGContextClearRect(layerContext1, self.bounds); CGContextDrawLayerInRect(layerContext1, self.bounds, self.layer2); CGContextRef layerContext = CGLayerGetContext(self.layer2 ); CGContextClearRect(layerContext, self.bounds); } – (void)pen1ButtonClicked { CGContextRef layerContext = CGLayerGetContext(self.layer2 ); CGContextClearRect(layerContext, self.bounds); CGContextDrawLayerInRect(layerContext, self.bounds, self.layer1); CGContextRef layerContext1 = CGLayerGetContext( self.layer1); CGContextClearRect(layerContext1, self.bounds); } 所以我把这个图层从一个图层转移到另一个图层,但是会发生什么 1)如果我画一条线,那么我点击擦除button,然后不擦除,如果我再次点击笔画,并没有画任何东西,如果我再次点击擦除button,现在当我试图擦除整个绘图消失 2)但是,如果我画线,然后点击擦除button,并删除线,然后再转移到笔button,画一条线,再次转到擦除button,并尝试擦除,它完美的工作。

iOS:我如何使用CGLayer支持Retina Display?

我在它的委托方法drawLayer:inContext:绘制一个CALayer图。 现在我想支持视网膜显示,因为graphics看起来最新的设备模糊。 对于我在CALayer传递的graphics上下文中直接绘制的部分,我可以通过如下设置CALayer的contentScale属性来很好地绘制高分辨率。 if ([myLayer respondsToSelector:@selector(setContentsScale:)]) { myLayer.contentsScale = [[UIScreen mainScreen] scale]; } 但是对于我使用CGLayer的部分仍然是模糊的。 如何在高分辨率的CGLayer上绘制视网膜显示? 我想使用CGLayer重复绘制graphics的相同绘graphics状,以及切断超过图层边缘的graphics线条。 我通过CGLayerCreateWithContex从CALayer传递的graphics上下文获取CGLayer,并使用CG函数(如CGContextFillPath或CGContextAddLineToPoint绘制其上下文。 我需要同时支持iOS 4.x和iOS 3.1.3,Retina和传统显示器。 谢谢, 库拉

CGLayerRef在NSValue – 什么时候调用retain()或release()?

我将一些graphicscaching到CGLayers上,然后使用@encode将它们存储在NSValue对象中(以便将它们存储在数组中)。 我只是想确保我正确处理保留/释放… 我cachinggraphics并将它们存储在数组中,如下所示: // Create an NSMutableArray "newCache" CGLayerRef drawingLayer = CGLayerCreateWithContext(context, bounds.size, NULL); CGContextRef drawingContext = CGLayerGetContext(drawingLayer); // Do some drawing… [newCache addObject:[NSValue valueWithBytes:&drawingLayer objCType:@encode(CGLayerRef)]]; CGLayerRelease(drawingLayer); // Is this release correct? 然后再检索图层: CGLayerRef retrievedLayer; [(NSValue *)[cacheArray objectAtIndex:index] getValue:&retrievedLayer]; // Use the layer… // Should I release retrievedLayer here? 我是否正确地认为图层在添加到数组之后需要释放(第一个代码片段中的最后一行)? 我认为这是因为我之前在代码中调用了一个创build函数。 NSValue然后跟踪我的图层数据? retrieveLayer是否需要在使用后手动释放? 谢谢

Coregraphics(iOS)中的内存pipe理

我正在绘制应用程序,我正在使用CGlayers进行绘图,所以我打开canvas,点击一个button, 我使用的UIBezierPath,然后将其转换为CGPath在touchesMoved下面,然后使用它来绘制 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { if (ctr == 4) { m_touchMoved = true; self.currentPath = [[DrawingPath alloc] init]; [self.currentPath setPathColor:self.lineColor]; self.currentPath.pathWidth = [NSString stringWithFormat:@"%f",self.lineWidth]; pts[3] = midPoint(pts[2], pts[4]);// move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of […]

撤销CGLayer的重做问题

我正在与CgLayer上的unodo重做操作,我已经尝试了一些代码,但不能够得到它的工作,不知道,我在哪里出错,下面是我的代码,我已经写 这是我的drawRect函数 – (void)drawRect:(CGRect)rect { m_backgroundImage = [UIImage imageNamed:@"bridge.jpg"]; CGPoint drawingTargetPoint = CGPointMake(0,0); [m_backgroundImage drawAtPoint:drawingTargetPoint]; switch(drawStep) { case DRAW: { CGContextRef context = UIGraphicsGetCurrentContext(); if(myLayerRef == nil) { myLayerRef = CGLayerCreateWithContext(context, self.bounds.size, NULL); } CGContextDrawLayerAtPoint(context, CGPointZero, myLayerRef); break; } case UNDO: { [curImage drawInRect:self.bounds]; break; } default: break; } } 在触摸结束时,我将图层转换为NSValue并存储为keyValue对NSDictionary,然后将字典对象添加到数组。 – (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent […]

使用Cglayer绘图进行撤消和重做

我正在使用绘图应用程序,我正在使用CGlayers进行绘图。 触摸结束后,我从图层中取出图像并将其存储在一个Array中,我使用该Array来撤消操作。 我的触摸结束function -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"Touches ended"); UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextDrawLayerInRect(context, self.bounds, self.drawingLayer); m_curImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); [m_undoArray addObject: m_curImage]; } 我的绘图视图根据用户需求dynamic扩展,所以假设用户可以绘制一个drawView大小为200 * 200的线,然后将其展开为200 * 300并再绘制一行,然后将其展开为200 * 300并再绘制一行。 这是应用程序的图像 所以现在我有3个不同大小的图像在UndoArray。 每当我增加/减lesscanvas大小。 我已经写了这个代码 在drawingView的增加和减less,我正在写这个function (void)increaseDecreaseDrawingView { self.currentDrawingLayer = nil; if(self.permanentDrawingLayer) { rectSize = self.bounds; NSLog(@"Size%@", NSStringFromCGRect(self.bounds)); CGContextRef context = […]

撤消/重做在iOS中进行绘图

我正在做一个绘图应用程序,我想要做撤销/重做,为此我保存CGPath的触摸结束了NSMutableArray,但我不理解如何渲染CGPaths点击撤消button EDIT1: 当我使用BezierPaths的时候,我首先决定用一个简单的方法去简单地去处理这个path,没有CGPath, 编辑2:因为我的撤消发生在段(我,部分而不是整个path被删除),我决定创build一个数组的数组,所以我做了相应的变化,现在我将在CGlayer中绘制,采取CGPath 所以这里“parentUndoArray”是数组的数组。 所以我这样做了 我有一个叫做Draw​​ingPath的类来完成绘图 //DrawingPath.h @interface DrawingPath : NSObject @property (strong, nonatomic) NSString *pathWidth; @property (strong,nonatomic) UIColor *pathColor; @property (strong,nonatomic) UIBezierPath *path; – (void)draw; @end //DrawingPath.m #import "DrawingPath.h" @implementation DrawingPath @synthesize pathWidth = _pathWidth; @synthesize pathColor = _pathColor; @synthesize path = _path; – (id)init { if (!(self = [super init] )) return […]