0

I am working with CgLayers for drawing in iOS, Now I want to store these layers and retrieve it later, so I want to know what are the available options. I tried to use NSMutableDictionary, below is the code,

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{   
    CGLayerRef testLayer = CGLayerCreateWithContext(UIGraphicsGetCurrentContext(),   self.bounds.size, NULL);

    CGContextRef context = CGLayerGetContext(testLayer);

    CGContextDrawLayerAtPoint(context, CGPointZero, myLayerRef); 

// here myLayerRef is my original layer


    NSValue *layerCopy = [NSValue valueWithPointer:testLayer];

    NSDictionary *lineInfo = [NSDictionary dictionaryWithObjectsAndKeys:layerCopy, @"IMAGE",
                              nil]; 

    [m_pathArray addObject:lineInfo];    

}

-(void) retrieve
{
    NSDictionary *lineInfo = [m_pathArray lastObject];


    NSValue *val = [lineInfo valueForKey:@"IMAGE"];

    CGLayerRef layerRef = (CGLayerRef) [val pointerValue];


    CGContextRef context1 = CGLayerGetContext(layerRef);
    CGContextDrawLayerAtPoint(context1, CGPointMake(00, 00),layerRef);
    [self setNeedsDisplayInRect:self.bounds];   
}

Regards Ranjit

Ranjit
  • 4,576
  • 11
  • 62
  • 121
  • Hello David, any suggestions? – Ranjit Jul 12 '12 at 11:23
  • Depending on the size of your view, this method will get really big really fast. A full screen view takes out several MB of memory. Anyway, your name says `layerCopy` but you realize...that doesn't make a copy at all right? In the end I think all of your NSValues are pointing to the same thing...To copy a CGLayer you must render it into the context of another CGLayer. No, I'm not going to give sample code. – borrrden Jul 13 '12 at 09:55
  • Hey thanks for replying, Yes you are right, I had previously checked that with log statements and each time I was getting, the same value for NSValue. Where do I need to render it into another context, in touches end function? And as you said its not the right approach, so may I know the right approach.:) – Ranjit Jul 13 '12 at 10:12
  • just before you put it in the dictionary – borrrden Jul 13 '12 at 10:36
  • Ok, but is this the optimal solution, as you said in the above comment that, it will take a lot of memory – Ranjit Jul 13 '12 at 10:37
  • Hey @borrrden, I have updated the code, according to your suggestions, please tell me whether I am right?Waiting for reply – Ranjit Jul 13 '12 at 10:45
  • Stop asking if you are right. If it works it is right, if it doesn't it is wrong. I assume it is not working though. You cannot call (UIGraphicsGetCurrentContext()) outside of drawRect and get anything meaningful like that. You need to get the context from the first layer. – borrrden Jul 13 '12 at 11:41
  • Ok, thanks, I asked it because I found very less documentation on CgLayers.Hey How about retrieving it? – Ranjit Jul 13 '12 at 11:55
  • Hey I changed the code, but its not working, I think my retrieve code is wrong – Ranjit Jul 13 '12 at 12:01
  • As a former fan and advocate of CGLayer, a note: I spoke with the Core Graphics team at WWDC. I asked when were the best situations to use CGLayer. Their answer was "never." It turns out that CGLayer has been quietly abandoned without being formally deprecated (and without updating the docs to indicate this). They now recommend CGBitmapContext for all purposes. – Rob Napier Jul 13 '12 at 13:29
  • Hi @Rob Napier, thanks for your valuable information. I was stuck with it, from few days, figuring it out from the base. Now as you said I should do all the drawing in CGBitmapContext right? But can you give me little more insights into how it should be done – Ranjit Jul 16 '12 at 07:05
  • I should have said CGBitmapContext or CALayer. CALayer is also often a good replacement for CGLayer (CGLayer predates it). Regarding CGBitmapContext, see Creating a Bitmap Context: https://developer.apple.com/library/mac/#documentation/graphicsimaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html – Rob Napier Jul 16 '12 at 14:56
  • Ok thanks Rob Napier, I will look at it, can you please look at this link http://stackoverflow.com/questions/11502320/eraser-not-working-in-ios-drawing . I am not getting what is going on – Ranjit Jul 17 '12 at 06:31

0 Answers0